package com.truyen.controller;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import nl.captcha.Captcha;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.AlbumEntry;
import com.truyen.utils.DateTimeUtils;
import com.truyen.utils.PagingInfo;
import com.truyen.utils.PicasawebAlbumUtil;
import com.truyen.utils.Unsigned;
import com.truyen.model.TapThoModel;
import com.truyen.persistence.entity.Account;
import com.truyen.persistence.entity.AnhDep;
import com.truyen.persistence.entity.ChapterTam;
import com.truyen.persistence.entity.ChapterTmpTruyenKiemHiep;
import com.truyen.persistence.entity.ChapterTmpTruyenMa;
import com.truyen.persistence.entity.ChapterTmpTruyenTinhCam;
import com.truyen.persistence.entity.ChapterTmpTruyenTrinhTham;
import com.truyen.persistence.entity.ChapterTruyenKiemHiep;
import com.truyen.persistence.entity.ChapterTruyenMa;
import com.truyen.persistence.entity.ChapterTruyenTinhCam;
import com.truyen.persistence.entity.ChapterTruyenTranh;
import com.truyen.persistence.entity.ChapterTmpTruyenTranh;
import com.truyen.persistence.entity.ChapterTruyenTrinhTham;
import com.truyen.persistence.entity.ContentAnhDep;
import com.truyen.persistence.entity.LoaiLog;
import com.truyen.persistence.entity.LoaiTruyenTranh;
import com.truyen.persistence.entity.LogJsoup;
import com.truyen.persistence.entity.LogOther;
import com.truyen.persistence.entity.SearchLoaiTruyenTranh;
import com.truyen.persistence.entity.TruyenKiemHiep;
import com.truyen.persistence.entity.TruyenMa;
import com.truyen.persistence.entity.TruyenTinhCam;
import com.truyen.persistence.entity.TruyenTranh;
import com.truyen.persistence.entity.TacGia;
import com.truyen.persistence.entity.TruyenTrinhTham;
import com.truyen.service.AnhDepService;
import com.truyen.service.ChapterTmpTruyenKiemHiepService;
import com.truyen.service.ChapterTmpTruyenMaService;
import com.truyen.service.ChapterTmpTruyenTinhCamService;
import com.truyen.service.ChapterTmpTruyenTrinhThamService;
import com.truyen.service.ChapterTruyenKiemHiepService;
import com.truyen.service.ChapterTruyenMaService;
import com.truyen.service.ChapterTruyenTinhCamService;
import com.truyen.service.ChapterTruyenTrinhThamService;
import com.truyen.service.ContentAnhDepService;
import com.truyen.service.JsoupService;
import com.truyen.service.LoaiTruyenTranhService;
import com.truyen.service.SearchLoaiTruyenTranhService;
import com.truyen.service.TacGiaService;
import com.truyen.service.ChapterTamService;
import com.truyen.service.ChapterTruyenTranhService;
import com.truyen.service.ChapterTmpTruyenTranhService;
import com.truyen.service.LoaiLogService;
import com.truyen.service.LogJsoupService;
import com.truyen.service.LogOtherService;
import com.truyen.service.TruyenKiemHiepService;
import com.truyen.service.TruyenMaService;
import com.truyen.service.TruyenTinhCamService;
import com.truyen.service.TruyenTranhService;
import com.truyen.service.TruyenTrinhThamService;
import com.truyen.service.UserService;


@Controller
@RequestMapping("/quan-ly")
@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
public class DataController {
	
	@Autowired
	private JsoupService jsoupService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private TacGiaService tacGiaService;
	
	@Autowired
	private LogJsoupService logJsoupService;
	
	@Autowired
	private LogOtherService logOtherService;
	
	@Autowired
	private LoaiLogService loaiLogService;
	
	@Autowired
	private LoaiTruyenTranhService loaiTruyenTranhService;
	
	@Autowired
	private SearchLoaiTruyenTranhService searchLoaiTruyenTranhService;
	
	@Autowired
	private TruyenTranhService truyenTranhService;
	
	@Autowired
	private ChapterTruyenTranhService chapterTruyenTranhService;
	
	@Autowired
	private ChapterTmpTruyenTranhService chapterTmpTruyenTranhService;
	
	@Autowired
	private TruyenKiemHiepService truyenKiemHiepService;
	
	@Autowired
	private ChapterTruyenKiemHiepService chapterTruyenKiemHiepService;
	
	@Autowired
	private ChapterTmpTruyenKiemHiepService chapterTmpTruyenKiemHiepService;
	
	@Autowired
	private TruyenTinhCamService truyenTinhCamService;
	
	@Autowired
	private TruyenMaService truyenMaService;
	
	@Autowired
	private ChapterTruyenTinhCamService chapterTruyenTinhCamService;
	
	@Autowired
	private ChapterTruyenMaService chapterTruyenMaService;
	
	@Autowired
	private ChapterTmpTruyenTinhCamService chapterTmpTruyenTinhCamService;
	
	@Autowired
	private ChapterTmpTruyenMaService chapterTmpTruyenMaService;
	
	@Autowired
	private TruyenTrinhThamService truyenTrinhThamService;
	
	@Autowired
	private ChapterTruyenTrinhThamService chapterTruyenTrinhThamService;
	
	@Autowired
	private ChapterTmpTruyenTrinhThamService chapterTmpTruyenTrinhThamService;
	
	@Autowired
	private ChapterTamService chapterTamService;
	
	@Autowired
	private AnhDepService anhDepService;
	
	@Autowired
	private ContentAnhDepService contentAnhDepService;
	
	@Autowired
	private Md5PasswordEncoder md5PasswordEncoder;
	
	/****************************************************
	 * 
	 * Account
	 * 
	 ****************************************************/
	
	// quan ly account
	@RequestMapping("/tai-khoan/quan-ly-tai-khoan.htm")
	public String quanLyTaiKhoan(Model model,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		List<Account> lstItem = userService
				.getAllLst(kwSearch, sort, sortBy, pInfo);
		model.addAttribute("kwSearch", kwSearch);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstItem", lstItem);
		return "/data/account/lstAccount";
	}
	
	// Tao moi account
	@RequestMapping("/tai-khoan/tao-tai-khoan.htm")
	public String createNewAccount(Model model) {
		return "/data/account/createNewAccount";
	}

	@RequestMapping("/tai-khoan/xu-ly-tao-tai-khoan.htm")
	public String processCreateNewAccount(Model model,HttpServletRequest request,
			@RequestParam(defaultValue = "2") int level,
			@RequestParam String email,
			@RequestParam String pwd,
			@RequestParam String nickName,
			@RequestParam String answer) throws Exception {
		String notice = "";
		if (level > 0 && email != null && !email.equals("") && pwd != null
				&& !pwd.equals("") && nickName != null && !nickName.equals("")){
			Captcha captcha = (Captcha) request.getSession().getAttribute("Captcha5");
			request.setCharacterEncoding("UTF-8"); // Do this so we can capture non-Latin chars
			if (captcha.isCorrect(answer)) {
				Account account = userService.findItemByProperty("email",email);
				if(account == null){
					account = userService.findItemByProperty("nickName", nickName);
					if(account == null){
						Date dayReg = new Date();
						boolean isActive = true;
						String avatar = "http://4.bp.blogspot.com/-49JcqMXrClA/UFTSKKEwmRI/AAAAAAAAAPQ/Bim-43ADo3g";
						account = new Account(email,md5PasswordEncoder.encodePassword(pwd, null),nickName,level,dayReg,isActive,avatar);
						userService.save(account);
						notice = "successfully";
					}else{
						notice = "nickNameAlreadyExists";
					}
				}else{
					notice = "emailAlreadyExists";
				}
			} else {
				model.addAttribute("notice", "Sai captcha!");
				model.addAttribute("email", email);
				model.addAttribute("pwd", pwd);
				model.addAttribute("nickName", nickName);
				return "/data/account/createNewAccount";
			}
		}else{
			notice ="error";
		}
		model.addAttribute("notice", notice);
		return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
	}
	
	// set isActive account
	@RequestMapping("/tai-khoan/isActive-tai-khoan.htm")
	public String isDeleteAccount(Model model,@RequestParam Long id) {
		if(id != null && id > 0){
			userService.updateBoolean(id,"isActive");
		}
		return "redirect:/quan-ly/tai-khoan/quan-ly-tai-khoan.htm";
	}
	
	// edit account
	@RequestMapping("/tai-khoan/edit-tai-khoan.htm")
	public String editAccount(Model model,@RequestParam Long id) {
		if(id != null && id > 0){
			Account account = userService.findById(id);
			model.addAttribute("account", account);
			return "/data/account/editAccount";
		}else{
			return "redirect:/quan-ly/tai-khoan/quan-ly-tai-khoan.htm";
		}
		
	}
	
	// xu ly edit account
	@RequestMapping("/tai-khoan/xu-ly-edit-tai-khoan.htm")
	public String processEditAccount(Model model,@RequestParam Long id,
			@RequestParam(defaultValue = "2") int level,
			@RequestParam(defaultValue = "") String fullName,
			@RequestParam(defaultValue = "") String avatar,
			@RequestParam(defaultValue = "") String address,
			@RequestParam(defaultValue = "") String mobile,
			@RequestParam(defaultValue = "true") boolean sex,
			@RequestParam(defaultValue = "") String birthDay,
			@RequestParam(defaultValue = "") long liked,
			@RequestParam(defaultValue = "") long toCoin) {
		if(id != null && id > 0){
			Date birthD = DateTimeUtils.StringDateToDate(birthDay);
			boolean flag = userService.updateAccount(id,level,fullName,avatar,address,mobile,sex,birthD,liked,toCoin);
			if(flag){
				model.addAttribute("notice", "successfully");
				return "redirect:/quan-ly/tai-khoan/quan-ly-tai-khoan.htm";
			}else{
				model.addAttribute("notice", "error");
				Account account = userService.findById(id);
				model.addAttribute("account", account);
				return "/data/account/editAccount";
			}
		}else{
			model.addAttribute("notice", "error");
			return "redirect:/quan-ly/tai-khoan/quan-ly-tai-khoan.htm";
		}
		
	}
	
	/****************************************************
	 * 
	 * log
	 * 
	 ****************************************************/
	
	//xem log jsoup
	@RequestMapping("/log/xem-log-jsoup.htm")
	public String xemLogJsoup(Model model,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "dateLog") String sortBy,
			@RequestParam(defaultValue = "jsoup blogtruyen") String typeLog,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		LoaiLog loaiLog = loaiLogService.findByProperty("alias", typeLog);
		List<LogJsoup> lstLogJsoup = logJsoupService
				.getLstLog(sort, sortBy, loaiLog, pInfo);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("typeLog", typeLog);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstLogJsoup", lstLogJsoup);
		return "/data/logJsoup/lstLog";
	}
	
	//xem log jsoup
	@RequestMapping("/log/xem-log-other.htm")
	public String xemLogOther(Model model,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "dateLog") String sortBy,
			@RequestParam(defaultValue = "doi chapter number") String typeLog,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		LoaiLog loaiLog = loaiLogService.findByProperty("alias", typeLog);
		List<LogOther> lstLogOther = logOtherService
				.getLstLog(sort, sortBy, loaiLog, pInfo);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("typeLog", typeLog);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstLogOther", lstLogOther);
		return "/data/logOther/lstLog";
	}
	
	//xoa log jsoup
	@RequestMapping("/log/xoa-log-jsoup.htm")
	public String xoaLogJsoup(Model model,
			@RequestParam boolean isError) {
		logJsoupService.delLst(isError);
		return "redirect:/quan-ly/log/xem-log-jsoup.htm";
	}
	
	//xoa log other
	@RequestMapping("/log/xoa-log-other.htm")
	public String xoaLogOther(Model model,
			@RequestParam boolean isError) {
		logOtherService.delLst(isError);
		return "redirect:/quan-ly/log/xem-log-other.htm";
	}
	
	//tao loai log
	@RequestMapping("/loai-log/tao-loai-log.htm")
	public String createTypeLog(Model model) {
		return "/data/loaiLog/createTypeLog";
	}
	
	//xu ly tao loai log
	@RequestMapping("/loai-log/xu-ly-tao-loai-log.htm")
	public String processCreateTypeLog(Model model,@RequestParam String name) {
		if(name!=null && !name.equals("")){
			String alias = Unsigned.boDau(name);
			LoaiLog loaiLog = loaiLogService.findByProperty("alias", alias);
			if(loaiLog == null){
				loaiLog = new LoaiLog(name,alias);
				loaiLogService.save(loaiLog);
				model.addAttribute("notice", "successfully");
			}else{
				model.addAttribute("notice", "loaiLogAlreadyExists");
			}
		}else{
			model.addAttribute("notice", "error");
		}
		return "redirect:/quan-ly/loai-log/tao-loai-log.htm";
	}
	
	//danh sach loai log
	@RequestMapping("/loai-log/danh-sach.htm")
	public String danhSachLoaiLog(Model model,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		List<LoaiLog> lstLoaiLog = loaiLogService
				.getAllLst(kwSearch, sort, sortBy, pInfo);
		model.addAttribute("kwSearch", kwSearch);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstLoaiLog", lstLoaiLog);
		return "/data/loaiLog/lstLoaiLog";
	}
	
	/****************************************************
	 * 
	 * Chapter tam
	 * 
	 ****************************************************/
	
	//danh sach tat ca chapter truyen
	@RequestMapping("/chapterTam/danh-sach-tat-ca-chapter-tam.htm")
	public String danhSachChapterTam(Model model,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		List<ChapterTam> lstChapterTam = chapterTamService
				.getLstAllChapterTam(kwSearch, sort, sortBy, pInfo);
		model.addAttribute("kwSearch", kwSearch);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstChapterTam", lstChapterTam);
		return "/data/chapterTam/lstAllChapterTam";
	}
	
	//doi chapternumber cua chapter tam
	@RequestMapping("/chapterTam/doi-chapterNumber.htm")
	public void changeChapterNumberTam(@RequestParam Long id,
			@RequestParam float chapterNumberChange){
		if(id > 0 && chapterNumberChange > 0){
			chapterTamService.updateChapterNumber(id,chapterNumberChange);
		}
	}
	
	//doi linkSource cua chapter tam
	@RequestMapping("/chapterTam/doi-linkSource.htm")
	public void changeLinkSourceTam(@RequestParam Long id,
			@RequestParam String linkSource){
		if(id > 0 && linkSource != null && !linkSource.equals("")){
			chapterTamService.updateLinkSource(id,linkSource);
		}
	}
	
	/****************************************************
	 * 
	 * Chapter tam truyen tranh
	 * 
	 ****************************************************/
	
	//danh sach chapter tam theo truyen
	@RequestMapping("/chapterTam/danh-sach-chapter-tam-theo-truyen.htm")
	public String danhSachChapterTamTheoTruyen(Model model,
			@RequestParam Long idTruyen,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		if(idTruyen != null && idTruyen > 0){
			TruyenTranh truyen = truyenTranhService.findById(idTruyen);
			if(truyen!=null){
				PagingInfo pInfo = new PagingInfo();
				pInfo.setCurrentPage(curPage);
				pInfo.setSizePerPage(sizePerPage);
				List<ChapterTam> lstChapterTam = chapterTamService
						.getLstChapterTamByTruyen(truyen, kwSearch, sort, sortBy, pInfo);
				model.addAttribute("idTruyen", idTruyen);
				model.addAttribute("kwSearch", kwSearch);
				model.addAttribute("totalSize", pInfo.getCount());
				model.addAttribute("sizePerPage", sizePerPage);
				model.addAttribute("curPage", curPage);
				model.addAttribute("sort", sort);
				model.addAttribute("sortBy", sortBy);
				model.addAttribute("pInfo", pInfo);
				model.addAttribute("lstChapterTam", lstChapterTam);
				return "/data/chapterTam/lstChapterTamByTruyen";
			}else{
				return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
			}
		}else{
			return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
		}
	}
	
	//cap nhat chapternumber theo chaptertruyen
	@RequestMapping("/chapterTam/cap-nhat-lai-chapter-number.htm")
	public String upadteLstChapterNumberChapterTam() {
		chapterTamService.updateLstChapterNumberBlogTruyen();
		return "redirect:/quan-ly/truyen-tranh/danh-sach.htm";
	}
	
	/****************************************************
	 * 
	 * Chapter tam truyen kiem hiep
	 * 
	 ****************************************************/
	
	//danh sach chapter tam theo truyen kiem hiep
	@RequestMapping("/chapterTamTruyenKiemHiep/danh-sach-chapter-tam-theo-truyen.htm")
	public String danhSachChapterTamTheoTruyenTruyenKiemHiep(Model model,
			@RequestParam Long idTruyen,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		if(idTruyen != null && idTruyen > 0){
			TruyenKiemHiep truyen = truyenKiemHiepService.findById(idTruyen);
			if(truyen!=null){
				PagingInfo pInfo = new PagingInfo();
				pInfo.setCurrentPage(curPage);
				pInfo.setSizePerPage(sizePerPage);
				List<ChapterTam> lstChapterTam = chapterTamService
						.getLstChapterTamByTruyen(truyen, kwSearch, sort, sortBy, pInfo);
				model.addAttribute("idTruyen", idTruyen);
				model.addAttribute("kwSearch", kwSearch);
				model.addAttribute("totalSize", pInfo.getCount());
				model.addAttribute("sizePerPage", sizePerPage);
				model.addAttribute("curPage", curPage);
				model.addAttribute("sort", sort);
				model.addAttribute("sortBy", sortBy);
				model.addAttribute("pInfo", pInfo);
				model.addAttribute("lstChapterTam", lstChapterTam);
				return "/data/chapterTamTruyenKiemHiep/lstChapterTamByTruyen";
			}else{
				return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
			}
		}else{
			return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
		}
	}
	
	/****************************************************
	 * 
	 * Chapter tam truyen ma
	 * 
	 ****************************************************/
	
	//danh sach chapter tam theo truyen kiem hiep
	@RequestMapping("/chapterTamTruyenMa/danh-sach-chapter-tam-theo-truyen.htm")
	public String danhSachChapterTamTheoTruyenTruyenMa(Model model,
			@RequestParam Long idTruyen,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		if(idTruyen != null && idTruyen > 0){
			TruyenMa truyen = truyenMaService.findById(idTruyen);
			if(truyen!=null){
				PagingInfo pInfo = new PagingInfo();
				pInfo.setCurrentPage(curPage);
				pInfo.setSizePerPage(sizePerPage);
				List<ChapterTam> lstChapterTam = chapterTamService
						.getLstChapterTamByTruyen(truyen, kwSearch, sort, sortBy, pInfo);
				model.addAttribute("idTruyen", idTruyen);
				model.addAttribute("kwSearch", kwSearch);
				model.addAttribute("totalSize", pInfo.getCount());
				model.addAttribute("sizePerPage", sizePerPage);
				model.addAttribute("curPage", curPage);
				model.addAttribute("sort", sort);
				model.addAttribute("sortBy", sortBy);
				model.addAttribute("pInfo", pInfo);
				model.addAttribute("lstChapterTam", lstChapterTam);
				return "/data/chapterTamTruyenMa/lstChapterTamByTruyen";
			}else{
				return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
			}
		}else{
			return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
		}
	}
	
	/****************************************************
	 * 
	 * Chapter tam truyen tinh cam
	 * 
	 ****************************************************/
	
	//danh sach chapter tam theo truyen tinh cam
	@RequestMapping("/chapterTamTruyenTinhCam/danh-sach-chapter-tam-theo-truyen.htm")
	public String danhSachChapterTamTheoTruyenTruyenTinhCam(Model model,
			@RequestParam Long idTruyen,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		if(idTruyen != null && idTruyen > 0){
			TruyenTinhCam truyen = truyenTinhCamService.findById(idTruyen);
			if(truyen!=null){
				PagingInfo pInfo = new PagingInfo();
				pInfo.setCurrentPage(curPage);
				pInfo.setSizePerPage(sizePerPage);
				List<ChapterTam> lstChapterTam = chapterTamService
						.getLstChapterTamByTruyen(truyen, kwSearch, sort, sortBy, pInfo);
				model.addAttribute("idTruyen", idTruyen);
				model.addAttribute("kwSearch", kwSearch);
				model.addAttribute("totalSize", pInfo.getCount());
				model.addAttribute("sizePerPage", sizePerPage);
				model.addAttribute("curPage", curPage);
				model.addAttribute("sort", sort);
				model.addAttribute("sortBy", sortBy);
				model.addAttribute("pInfo", pInfo);
				model.addAttribute("lstChapterTam", lstChapterTam);
				return "/data/chapterTamTruyenTinhCam/lstChapterTamByTruyen";
			}else{
				return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
			}
		}else{
			return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
		}
	}
	
	/****************************************************
	 * 
	 * Chapter tam truyen trinh tham
	 * 
	 ****************************************************/
	
	//danh sach chapter tam theo truyen trinh tham
	@RequestMapping("/chapterTamTruyenTrinhTham/danh-sach-chapter-tam-theo-truyen.htm")
	public String danhSachChapterTamTheoTruyenTruyenTrinhTham(Model model,
			@RequestParam Long idTruyen,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		if(idTruyen != null && idTruyen > 0){
			TruyenTrinhTham truyen = truyenTrinhThamService.findById(idTruyen);
			if(truyen!=null){
				PagingInfo pInfo = new PagingInfo();
				pInfo.setCurrentPage(curPage);
				pInfo.setSizePerPage(sizePerPage);
				List<ChapterTam> lstChapterTam = chapterTamService
						.getLstChapterTamByTruyen(truyen, kwSearch, sort, sortBy, pInfo);
				model.addAttribute("idTruyen", idTruyen);
				model.addAttribute("kwSearch", kwSearch);
				model.addAttribute("totalSize", pInfo.getCount());
				model.addAttribute("sizePerPage", sizePerPage);
				model.addAttribute("curPage", curPage);
				model.addAttribute("sort", sort);
				model.addAttribute("sortBy", sortBy);
				model.addAttribute("pInfo", pInfo);
				model.addAttribute("lstChapterTam", lstChapterTam);
				return "/data/chapterTamTruyenTrinhTham/lstChapterTamByTruyen";
			}else{
				return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
			}
		}else{
			return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
		}
	}
	
	/****************************************************
	 * 
	 * truyen tranh
	 * 
	 ****************************************************/
	
	//danh sach truyen
	@RequestMapping("/truyen-tranh/danh-sach.htm")
	public String danhSachTruyen(Model model,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		List<TruyenTranh> lstTruyen = truyenTranhService
				.getAllLst(kwSearch, sort, sortBy, pInfo);
		model.addAttribute("kwSearch", kwSearch);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstTruyen", lstTruyen);
		return "/data/truyenTranh/lstTruyen";
	}
	
	//chinh sua - them truyen
	@RequestMapping("/truyen-tranh/chinh-sua-truyen.htm")
	public String chinhSuaTruyen(Model model,@RequestParam(defaultValue="") Long id){
		if(id != null && id > 0){
			TruyenTranh truyen = truyenTranhService.findById(id);
			if(truyen==null){
				return "redirect:/quan-ly/truyen-tranh/danh-sach.htm";
			}
			model.addAttribute("truyen", truyen);
		}
		return "/data/truyenTranh/addEditTruyen";
	}
	
	//xu ly them truyen
	@RequestMapping("/truyen-tranh/xu-ly-them-truyen.htm")
	public String addTruyen(Model model,
			@RequestParam(defaultValue="") Long idAccount,
			@RequestParam(defaultValue="") String name,
			@RequestParam(defaultValue="") String linkImage,
			@RequestParam(defaultValue="") String nameImage,
			@RequestParam(defaultValue="") String info,
			@RequestParam(defaultValue="") String author,
			@RequestParam(defaultValue="") String categories,
			@RequestParam(defaultValue="") float totalChapter,
			@RequestParam(defaultValue="") boolean isDelete,
			@RequestParam(defaultValue="") boolean isFinished,
			@RequestParam(defaultValue="") boolean isHot,
			@RequestParam(defaultValue="") long liked,
			@RequestParam(defaultValue="") long views,
			@RequestParam(defaultValue="") long viewsWeek,
			@RequestParam(defaultValue="") long viewsMonth,
			@RequestParam(defaultValue="") int sourceBy,
			@RequestParam(defaultValue="") String linkSource,
			@RequestParam(defaultValue="") String translator){
		if(idAccount!=null && idAccount > 0 && name != null && !name.equals("")){
			name = name.replace("script", "");
			String alias = Unsigned.boDau(name);
			TruyenTranh truyen = truyenTranhService.findByProperty("alias", alias);
			if(truyen == null){
				Account acc = userService.findById(idAccount);
				linkImage=linkImage.replace("script", "");
				nameImage=nameImage.replace("script", "");
				info=info.replace("script", "");
				author=author.replace("script", "");
				String authorAlias = Unsigned.boDau(author);
				TacGia tacGia = tacGiaService.findByAliasLoaiTruyen(authorAlias,1);
				if(tacGia == null){
					tacGia = new TacGia(author,authorAlias,1);
					tacGiaService.save(tacGia);
				}
				categories=categories.replace("script", "");
				linkSource=linkSource.replace("script", "");
				translator=translator.replace("script", "");
				truyen = new TruyenTranh(name, alias, linkImage, nameImage, info, tacGia, categories, totalChapter, isDelete, isFinished, isHot, liked, views, viewsWeek, viewsMonth, sourceBy, linkSource, translator, new Date(),acc);
				truyenTranhService.save(truyen);
				String[] temp = categories.split(",");
				for(int j=0; j < temp.length; j++){
					String category = temp[j].trim();
					String aliasCategory = Unsigned.boDau(category);
					LoaiTruyenTranh loaiTruyenTranh = loaiTruyenTranhService.findByProperty("alias", aliasCategory);
					if(loaiTruyenTranh == null){
						loaiTruyenTranh = new LoaiTruyenTranh(category,aliasCategory);
						loaiTruyenTranhService.save(loaiTruyenTranh);
					}
					SearchLoaiTruyenTranh sltt = new SearchLoaiTruyenTranh(loaiTruyenTranh,truyen);
					searchLoaiTruyenTranhService.save(sltt);
				}
				model.addAttribute("notice", "successfully");
			}
			return "redirect:/quan-ly/truyen-tranh/danh-sach.htm";
		}else{
			model.addAttribute("notice", "error");
			return "redirect:/quan-ly/truyen-tranh/chinh-sua-truyen.htm";
		}
	}
	
	//xu ly sua truyen
	@RequestMapping("/truyen-tranh/xu-ly-chinh-sua-truyen.htm")
	public String editTruyen(Model model,
			@RequestParam(defaultValue="") Long idTruyen,
			@RequestParam(defaultValue="") String name,
			@RequestParam(defaultValue="") String linkImage,
			@RequestParam(defaultValue="") String nameImage,
			@RequestParam(defaultValue="") String info,
			@RequestParam(defaultValue="") String author,
			@RequestParam(defaultValue="") String categories,
			@RequestParam(defaultValue="") float totalChapter,
			@RequestParam(defaultValue="") boolean isDelete,
			@RequestParam(defaultValue="") boolean isFinished,
			@RequestParam(defaultValue="") boolean isHot,
			@RequestParam(defaultValue="") long liked,
			@RequestParam(defaultValue="") long views,
			@RequestParam(defaultValue="") long viewsWeek,
			@RequestParam(defaultValue="") long viewsMonth,
			@RequestParam(defaultValue="") int sourceBy,
			@RequestParam(defaultValue="") String linkSource,
			@RequestParam(defaultValue="") String translator){
		if(idTruyen != null && idTruyen > 0 && name != null && !name.equals("")){
			name = name.replace("script", "");
			String alias = Unsigned.boDau(name);
			TruyenTranh truyen = truyenTranhService.findByProperty("alias", alias);
			if(truyen != null && truyen.getId() != idTruyen){
				linkImage=linkImage.replace("script", "");
				nameImage=nameImage.replace("script", "");
				info=info.replace("script", "");
				author=author.replace("script", "");
				String authorAlias = Unsigned.boDau(author);
				TacGia tacGia = tacGiaService.findByAliasLoaiTruyen(authorAlias,1);
				if(tacGia == null){
					tacGia = new TacGia(author,authorAlias,1);
					tacGiaService.save(tacGia);
				}
				categories=categories.replace("script", "");
				linkSource=linkSource.replace("script", "");
				translator=translator.replace("script", "");
				truyenTranhService.update(idTruyen, name, alias, linkImage, nameImage, info, tacGia, categories, totalChapter, isDelete, isFinished, isHot, liked, views, viewsWeek, viewsMonth, sourceBy, linkSource, translator);
				model.addAttribute("notice", "successfully");
			}
		}else{
			model.addAttribute("notice", "error");
			return "redirect:/quan-ly/truyen-tranh/chinh-sua-truyen.htm?id="+idTruyen;
		}
		return "redirect:/quan-ly/truyen-tranh/danh-sach.htm";
	}
	
	//set hot truyen
	@RequestMapping("/truyen-tranh/set-hot.htm")
	public String setTruyenHot(@RequestParam Long id){
		if(id != null && id > 0){
			truyenTranhService.setHot(id);
		}
		return "redirect:/quan-ly/truyen-tranh/danh-sach.htm";
	}
	
	//set isDelete truyen
	@RequestMapping("/truyen-tranh/set-isDelete-truyen.htm")
	public String setIsDeleteTruyen(Model model,@RequestParam Long id){
		if(id != null && id > 0){
			truyenTranhService.setIsDelete(id);
			model.addAttribute("notice", "successfully");
		}else{
			model.addAttribute("notice", "error");
		}
		return "redirect:/quan-ly/truyen-tranh/danh-sach.htm";
	}
	
	//xoa truyen
	@RequestMapping("/truyen-tranh/xoa-truyen.htm")
	public String deleteTruyen(Model model, @RequestParam Long id){
		if(id != null && id > 0){
			truyenTranhService.delete(id);
			model.addAttribute("notice", "successfully");
		}else{
			model.addAttribute("notice", "error");
		}
		return "redirect:/quan-ly/truyen-tranh/danh-sach.htm";
	}
	
	//doi totalChapter
	@RequestMapping("/truyen-tranh/doi-totalChapter.htm")
	public void changeTotalChapter(@RequestParam Long id,
			@RequestParam float totalChapter){
		if(id > 0 && totalChapter > 0){
			truyenTranhService.updateTotalChapterr(id,totalChapter);
		}
	}
	
	//set isDelete chapter truyen
	@RequestMapping("/chapterTruyenTranh/set-isDelete-chapter-truyen.htm")
	public String setIsDeleteChapterTruyen(@RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
		String rs = "redirect:/quan-ly/chapterTruyenTranh/danh-sach-tat-ca-chapter-truyen.htm";
		if(id != null && id > 0){
			chapterTruyenTranhService.setIsDelete(id);
			if(idTruyen != null && idTruyen > 0){
				rs = "redirect:/quan-ly/chapterTruyenTranh/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
			}
		}
		return rs;
	}
	
	//danh sach chapter truyen theo truyen
	@RequestMapping("/chapterTruyenTranh/danh-sach-theo-truyen.htm")
	public String danhSachChapterTheoTruyen(Model model,
			@RequestParam Long idTruyen,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "chapterNumber") String sortBy,
			@RequestParam(defaultValue = "notNull") String contentIsNull,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		if(idTruyen != null && idTruyen > 0){
			TruyenTranh truyen = truyenTranhService.findById(idTruyen);
			if(truyen!=null){
				PagingInfo pInfo = new PagingInfo();
				pInfo.setCurrentPage(curPage);
				pInfo.setSizePerPage(sizePerPage);
				List<ChapterTmpTruyenTranh> lstChapterTruyen = chapterTmpTruyenTranhService
						.getLstChapterByTruyen(truyen, kwSearch, sort, sortBy, pInfo);
				model.addAttribute("idTruyen", idTruyen);
				model.addAttribute("kwSearch", kwSearch);
				model.addAttribute("totalSize", pInfo.getCount());
				model.addAttribute("sizePerPage", sizePerPage);
				model.addAttribute("curPage", curPage);
				model.addAttribute("sort", sort);
				model.addAttribute("sortBy", sortBy);
				model.addAttribute("contentIsNull", contentIsNull);
				model.addAttribute("pInfo", pInfo);
				model.addAttribute("lstChapterTruyen", lstChapterTruyen);
				return "/data/chapterTruyenTranh/lstChapterByTruyen";
			}else{
				return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
			}
		}else{
			return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
		}
	}
	
	//danh sach tat ca chapter truyen
	@RequestMapping("/chapterTruyenTranh/danh-sach-tat-ca-chapter-truyen.htm")
	public String danhSachChapterTruyen(Model model,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		List<ChapterTmpTruyenTranh> lstChapterTruyen = chapterTmpTruyenTranhService
				.getLstAllChapterTmpTruyenTranh(kwSearch, sort, sortBy, pInfo);
		model.addAttribute("kwSearch", kwSearch);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstChapterTruyen", lstChapterTruyen);
		return "/data/chapterTruyenTranh/lstAllChapterTruyen";
	}
	
	//doi chapternumber cua chapter truyen
	@RequestMapping("/chapterTruyenTranh/doi-chapterNumber.htm")
	public void changeChapterNumberTruyen(@RequestParam Long id,
			@RequestParam float chapterNumberChange){
		if(id > 0 && chapterNumberChange > 0){
			chapterTruyenTranhService.updateChapterNumber(id,chapterNumberChange);
			chapterTmpTruyenTranhService.updateChapterNumber(id,chapterNumberChange);
		}
	}
	
	//chinh sua chapter truyen
	@RequestMapping("/chapterTruyenTranh/cap-nhat-chapter-truyen.htm")
	public String editChapterTruyen(Model model, @RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
		if(id!=null && id > 0){
			ChapterTruyenTranh chapter = chapterTruyenTranhService.findById(id);
			model.addAttribute("chapter", chapter);
			if(idTruyen!=null && idTruyen > 0){
				model.addAttribute("idTruyen", idTruyen);
			}
			return "/data/chapterTruyenTranh/editChapter";
		}else{
			return "redirect:/quan-ly/truyen-tranh/danh-sach.htm";
		}
	}
	
	//xu ly chinh sua chapter truyen
	@RequestMapping("/chapterTruyenTranh/xu-ly-cap-nhat-chapter.htm")
	public String processEditChapterTruyen(Model model,
			@RequestParam Long id,
			@RequestParam(defaultValue="") Long idTruyen,
			@RequestParam(defaultValue="") String name,
			@RequestParam(defaultValue="") Float chapterNumber,
			@RequestParam(defaultValue="") String content,
			@RequestParam(defaultValue="") boolean isDelete,
			@RequestParam(defaultValue="") boolean isHot) {
		if(id!=null && id > 0 && chapterNumber!=null && chapterNumber > 0 && content!=null && !content.equals("")){
			ChapterTruyenTranh chapter = chapterTruyenTranhService.findById(id);
			if(chapter != null){
				chapterTmpTruyenTranhService.update(id, chapterNumber);
				chapterTruyenTranhService.update(id, chapterNumber, content, isDelete, isHot);
				model.addAttribute("notice", "successfully");
			}else{
				model.addAttribute("notice", "error");
			}
		}else{
			model.addAttribute("notice", "error");
		}
		if(idTruyen != null){
			return "redirect:/quan-ly/chapterTruyenTranh/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
		}else{
			return "redirect:/quan-ly/chapterTruyenTranh/danh-sach-tat-ca-chapter-truyen.htm";
		}
	}
	
	//cap nhat chapternumber theo chaptertruyen
	@RequestMapping("/chapterTruyenTranh/cap-nhat-lai-chapter-number.htm")
	public String upadteLsChapterNumbertChapterTruyen() {
		chapterTruyenTranhService.updateLstChapterNumberBlogTruyen();
		return "redirect:/quan-ly/truyen-tranh/danh-sach.htm";
	}
	
	/****************************************************
	 * 
	 * Truyen kiem hiep
	 * 
	 ****************************************************/
	
	//danh sach truyen
	@RequestMapping("/truyen-kiem-hiep/danh-sach.htm")
	public String danhSachTruyenKiemHiep(Model model,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		List<TruyenKiemHiep> lstTruyen = truyenKiemHiepService
				.getAllLst(kwSearch, sort, sortBy, pInfo);
		model.addAttribute("kwSearch", kwSearch);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstTruyen", lstTruyen);
		return "/data/truyenKiemHiep/lstTruyen";
	}
	
	//chinh sua - them truyen
	@RequestMapping("/truyen-kiem-hiep/chinh-sua-truyen.htm")
	public String chinhSuaTruyenKiemHiep(Model model,@RequestParam(defaultValue="") Long id){
		if(id != null && id > 0){
			TruyenKiemHiep truyen = truyenKiemHiepService.findById(id);
			if(truyen==null){
				return "redirect:/quan-ly/truyen-kiem-hiep/danh-sach.htm";
			}
			model.addAttribute("truyen", truyen);
		}
		return "/data/truyenKiemHiep/addEditTruyen";
	}
	
	//xu ly them truyen
	@RequestMapping("/truyen-kiem-hiep/xu-ly-them-truyen.htm")
	public String addTruyenKiemHiep(Model model,
			@RequestParam(defaultValue="") Long idAccount,
			@RequestParam(defaultValue="") String name,
			@RequestParam(defaultValue="") String linkImage,
			@RequestParam(defaultValue="") String nameImage,
			@RequestParam(defaultValue="") String info,
			@RequestParam(defaultValue="") String author,
			@RequestParam(defaultValue="") float totalChapter,
			@RequestParam(defaultValue="") boolean isDelete,
			@RequestParam(defaultValue="") boolean isFinished,
			@RequestParam(defaultValue="") boolean isHot,
			@RequestParam(defaultValue="") long liked,
			@RequestParam(defaultValue="") long views,
			@RequestParam(defaultValue="") long viewsWeek,
			@RequestParam(defaultValue="") long viewsMonth,
			@RequestParam(defaultValue="") int sourceBy,
			@RequestParam(defaultValue="") String linkSource,
			@RequestParam(defaultValue="") String translator){
		if(idAccount!=null && idAccount > 0 && name != null && !name.equals("")){
			name = name.replace("script", "");
			String alias = Unsigned.boDau(name);
			TruyenKiemHiep truyen = truyenKiemHiepService.findByProperty("alias", alias);
			if(truyen == null){
				Account acc = userService.findById(idAccount);
				linkImage=linkImage.replace("script", "");
				nameImage=nameImage.replace("script", "");
				info=info.replace("script", "");
				author=author.replace("script", "");
				String authorAlias = Unsigned.boDau(author);
				TacGia tacGia = tacGiaService.findByAliasLoaiTruyen(authorAlias,1);
				if(tacGia == null){
					tacGia = new TacGia(author,authorAlias,1);
					tacGiaService.save(tacGia);
				}
				linkSource=linkSource.replace("script", "");
				translator=translator.replace("script", "");
				truyen = new TruyenKiemHiep(name, alias, linkImage, nameImage, info, tacGia, totalChapter, isDelete, isFinished, isHot, liked, views, viewsWeek, viewsMonth, sourceBy, linkSource, translator, new Date(),acc);
				truyenKiemHiepService.save(truyen);
				model.addAttribute("notice", "successfully");
			}
			return "redirect:/quan-ly/truyen-kiem-hiep/danh-sach.htm";
		}else{
			model.addAttribute("notice", "error");
			return "redirect:/quan-ly/truyen-kiem-hiep/chinh-sua-truyen.htm";
		}
	}
	
	//xu ly sua truyen
	@RequestMapping("/truyen-kiem-hiep/xu-ly-chinh-sua-truyen.htm")
	public String editTruyenKiemHiep(Model model,
			@RequestParam(defaultValue="") Long idTruyen,
			@RequestParam(defaultValue="") String name,
			@RequestParam(defaultValue="") String linkImage,
			@RequestParam(defaultValue="") String nameImage,
			@RequestParam(defaultValue="") String info,
			@RequestParam(defaultValue="") String author,
			@RequestParam(defaultValue="") String categories,
			@RequestParam(defaultValue="") float totalChapter,
			@RequestParam(defaultValue="") boolean isDelete,
			@RequestParam(defaultValue="") boolean isFinished,
			@RequestParam(defaultValue="") boolean isHot,
			@RequestParam(defaultValue="") long liked,
			@RequestParam(defaultValue="") long views,
			@RequestParam(defaultValue="") long viewsWeek,
			@RequestParam(defaultValue="") long viewsMonth,
			@RequestParam(defaultValue="") int sourceBy,
			@RequestParam(defaultValue="") String linkSource,
			@RequestParam(defaultValue="") String translator){
		if(idTruyen != null && idTruyen > 0 && name != null && !name.equals("")){
			name = name.replace("script", "");
			String alias = Unsigned.boDau(name);
			TruyenKiemHiep truyen = truyenKiemHiepService.findByProperty("alias", alias);
			if(truyen != null && truyen.getId() != idTruyen){
				linkImage=linkImage.replace("script", "");
				nameImage=nameImage.replace("script", "");
				info=info.replace("script", "");
				author=author.replace("script", "");
				String authorAlias = Unsigned.boDau(author);
				TacGia tacGia = tacGiaService.findByAliasLoaiTruyen(authorAlias,1);
				if(tacGia == null){
					tacGia = new TacGia(author,authorAlias,1);
					tacGiaService.save(tacGia);
				}
				categories=categories.replace("script", "");
				linkSource=linkSource.replace("script", "");
				translator=translator.replace("script", "");
				truyenKiemHiepService.update(idTruyen, name, alias, linkImage, nameImage, info, tacGia, categories, totalChapter, isDelete, isFinished, isHot, liked, views, viewsWeek, viewsMonth, sourceBy, linkSource, translator);
				model.addAttribute("notice", "successfully");
			}
		}else{
			model.addAttribute("notice", "error");
			return "redirect:/quan-ly/truyen-kiem-hiep/chinh-sua-truyen.htm?id="+idTruyen;
		}
		return "redirect:/quan-ly/truyen-kiem-hiep/danh-sach.htm";
	}
	
	//set hot truyen
	@RequestMapping("/truyen-kiem-hiep/set-hot.htm")
	public String setTruyenKiemHiepHot(@RequestParam Long id){
		if(id != null && id > 0){
			truyenKiemHiepService.setHot(id);
		}
		return "redirect:/quan-ly/truyen-kiem-hiep/danh-sach.htm";
	}
	
	//set isDelete truyen
	@RequestMapping("/truyen-kiem-hiep/set-isDelete-truyen.htm")
	public String setIsDeleteTruyenKiemHiep(Model model,@RequestParam Long id){
		if(id != null && id > 0){
			truyenKiemHiepService.setIsDelete(id);
			model.addAttribute("notice", "successfully");
		}else{
			model.addAttribute("notice", "error");
		}
		return "redirect:/quan-ly/truyen-kiem-hiep/danh-sach.htm";
	}
	
	//xoa truyen
	@RequestMapping("/truyen-kiem-hiep/xoa-truyen.htm")
	public String deleteTruyenKiemHiep(Model model, @RequestParam Long id){
		if(id != null && id > 0){
			truyenKiemHiepService.delete(id);
			model.addAttribute("notice", "successfully");
		}else{
			model.addAttribute("notice", "error");
		}
		return "redirect:/quan-ly/truyen-kiem-hiep/danh-sach.htm";
	}
	
	//doi totalChapter
	@RequestMapping("/truyen-kiem-hiep/doi-totalChapter.htm")
	public void changeTotalChapterTruyenKiemHiep(@RequestParam Long id,
			@RequestParam float totalChapter){
		if(id > 0 && totalChapter > 0){
			truyenKiemHiepService.updateTotalChapterr(id,totalChapter);
		}
	}
	
	//set isDelete chapter truyen
	@RequestMapping("/chapterTruyenKiemHiep/set-isDelete-chapter-truyen.htm")
	public String setIsDeleteChapterTruyenKiemHiep(@RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
		String rs = "redirect:/quan-ly/chapterTruyenKiemHiep/danh-sach-tat-ca-chapter-truyen.htm";
		if(id != null && id > 0){
			chapterTruyenKiemHiepService.setIsDelete(id);
			if(idTruyen != null && idTruyen > 0){
				rs = "redirect:/quan-ly/chapterTruyenKiemHiep/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
			}
		}
		return rs;
	}
	
	//danh sach chapter truyen theo truyen
	@RequestMapping("/chapterTruyenKiemHiep/danh-sach-theo-truyen.htm")
	public String danhSachChapterTheoTruyenKiemHiep(Model model,
			@RequestParam Long idTruyen,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "chapterNumber") String sortBy,
			@RequestParam(defaultValue = "notNull") String contentIsNull,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		if(idTruyen != null && idTruyen > 0){
			TruyenKiemHiep truyen = truyenKiemHiepService.findById(idTruyen);
			if(truyen!=null){
				PagingInfo pInfo = new PagingInfo();
				pInfo.setCurrentPage(curPage);
				pInfo.setSizePerPage(sizePerPage);
				List<ChapterTmpTruyenKiemHiep> lstChapterTruyen = chapterTmpTruyenKiemHiepService
						.getLstChapterByTruyen(truyen, kwSearch, sort, sortBy, pInfo);
				model.addAttribute("idTruyen", idTruyen);
				model.addAttribute("kwSearch", kwSearch);
				model.addAttribute("totalSize", pInfo.getCount());
				model.addAttribute("sizePerPage", sizePerPage);
				model.addAttribute("curPage", curPage);
				model.addAttribute("sort", sort);
				model.addAttribute("sortBy", sortBy);
				model.addAttribute("contentIsNull", contentIsNull);
				model.addAttribute("pInfo", pInfo);
				model.addAttribute("lstChapterTruyen", lstChapterTruyen);
				return "/data/chapterTruyenKiemHiep/lstChapterByTruyen";
			}else{
				return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
			}
		}else{
			return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
		}
	}
	
	//danh sach tat ca chapter truyen
	@RequestMapping("/chapterTruyenKiemHiep/danh-sach-tat-ca-chapter-truyen.htm")
	public String danhSachChapterTruyenKiemHiep(Model model,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		List<ChapterTmpTruyenKiemHiep> lstChapterTruyen = chapterTmpTruyenKiemHiepService
				.getLstAllChapterTmpTruyenKiemHiep(kwSearch, sort, sortBy, pInfo);
		model.addAttribute("kwSearch", kwSearch);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstChapterTruyen", lstChapterTruyen);
		return "/data/chapterTruyenKiemHiep/lstAllChapterTruyen";
	}
	
	//doi chapternumber cua chapter truyen
	@RequestMapping("/chapterTruyenKiemHiep/doi-chapterNumber.htm")
	public void changeChapterNumberTruyenKiemHiep(@RequestParam Long id,
			@RequestParam float chapterNumberChange){
		if(id > 0 && chapterNumberChange > 0){
			chapterTruyenKiemHiepService.updateChapterNumber(id,chapterNumberChange);
			chapterTmpTruyenKiemHiepService.updateChapterNumber(id,chapterNumberChange);
		}
	}
	
	//chinh sua chapter truyen
	@RequestMapping("/chapterTruyenKiemHiep/cap-nhat-chapter-truyen.htm")
	public String editChapterTruyenKiemHiep(Model model, @RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
		if(id!=null && id > 0){
			ChapterTruyenKiemHiep chapter = chapterTruyenKiemHiepService.findById(id);
			model.addAttribute("chapter", chapter);
			if(idTruyen!=null && idTruyen > 0){
				model.addAttribute("idTruyen", idTruyen);
			}
			return "/data/chapterTruyenKiemHiep/editChapter";
		}else{
			return "redirect:/quan-ly/truyen-kiem-hiep/danh-sach.htm";
		}
	}
	
	//xu ly chinh sua chapter truyen
	@RequestMapping("/chapterTruyenKiemHiep/xu-ly-cap-nhat-chapter.htm")
	public String processEditChapterTruyenKiemHiep(Model model,
			@RequestParam Long id,
			@RequestParam(defaultValue="") Long idTruyen,
			@RequestParam(defaultValue="") String name,
			@RequestParam(defaultValue="") Float chapterNumber,
			@RequestParam(defaultValue="") String content,
			@RequestParam(defaultValue="") boolean isDelete,
			@RequestParam(defaultValue="") boolean isHot) {
		if(id!=null && id > 0 && chapterNumber!=null && chapterNumber > 0 && content!=null && !content.equals("")){
			ChapterTruyenKiemHiep chapter = chapterTruyenKiemHiepService.findById(id);
			if(chapter != null){
				chapterTmpTruyenKiemHiepService.update(id, chapterNumber);
				chapterTruyenKiemHiepService.update(id, chapterNumber, content, isDelete, isHot);
				model.addAttribute("notice", "successfully");
			}else{
				model.addAttribute("notice", "error");
			}
		}else{
			model.addAttribute("notice", "error");
		}
		if(idTruyen != null){
			return "redirect:/quan-ly/chapterTruyenKiemHiep/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
		}else{
			return "redirect:/quan-ly/chapterTruyenKiemHiep/danh-sach-tat-ca-chapter-truyen.htm";
		}
	}
	
	//cap nhat chapternumber theo chaptertruyen
	@RequestMapping("/chapterTruyenKiemHiep/cap-nhat-lai-chapter-number.htm")
	public String upadteLsChapterNumbertChapterTruyenKiemHiep() {
		chapterTruyenKiemHiepService.updateLstChapterNumberBlogTruyen();
		return "redirect:/quan-ly/truyen-kiem-hiep/danh-sach.htm";
	}
	
	/****************************************************
	 * 
	 * Truyen Tinh Cam
	 * 
	 ****************************************************/
	
	//danh sach truyen
	@RequestMapping("/truyen-tinh-cam/danh-sach.htm")
	public String danhSachTruyenTinhCam(Model model,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		List<TruyenTinhCam> lstTruyen = truyenTinhCamService
				.getAllLst(kwSearch, sort, sortBy, pInfo);
		model.addAttribute("kwSearch", kwSearch);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstTruyen", lstTruyen);
		return "/data/truyenTinhCam/lstTruyen";
	}
	
	//chinh sua - them truyen
	@RequestMapping("/truyen-tinh-cam/chinh-sua-truyen.htm")
	public String chinhSuaTruyenTinhCam(Model model,@RequestParam(defaultValue="") Long id){
		if(id != null && id > 0){
			TruyenTinhCam truyen = truyenTinhCamService.findById(id);
			if(truyen==null){
				return "redirect:/quan-ly/truyen-tinh-cam/danh-sach.htm";
			}
			model.addAttribute("truyen", truyen);
		}
		return "/data/truyenTinhCam/addEditTruyen";
	}
	
	//xu ly them truyen
	@RequestMapping("/truyen-tinh-cam/xu-ly-them-truyen.htm")
	public String addTruyenTinhCam(Model model,
			@RequestParam(defaultValue="") Long idAccount,
			@RequestParam(defaultValue="") String name,
			@RequestParam(defaultValue="") String linkImage,
			@RequestParam(defaultValue="") String nameImage,
			@RequestParam(defaultValue="") String info,
			@RequestParam(defaultValue="") String author,
			@RequestParam(defaultValue="") float totalChapter,
			@RequestParam(defaultValue="") boolean isDelete,
			@RequestParam(defaultValue="") boolean isFinished,
			@RequestParam(defaultValue="") boolean isHot,
			@RequestParam(defaultValue="") long liked,
			@RequestParam(defaultValue="") long views,
			@RequestParam(defaultValue="") long viewsWeek,
			@RequestParam(defaultValue="") long viewsMonth,
			@RequestParam(defaultValue="") int sourceBy,
			@RequestParam(defaultValue="") String linkSource,
			@RequestParam(defaultValue="") String translator){
		if(idAccount!=null && idAccount > 0 && name != null && !name.equals("")){
			name = name.replace("script", "");
			String alias = Unsigned.boDau(name);
			TruyenTinhCam truyen = truyenTinhCamService.findByProperty("alias", alias);
			if(truyen == null){
				Account acc = userService.findById(idAccount);
				linkImage=linkImage.replace("script", "");
				nameImage=nameImage.replace("script", "");
				info=info.replace("script", "");
				author=author.replace("script", "");
				String authorAlias = Unsigned.boDau(author);
				TacGia tacGia = tacGiaService.findByAliasLoaiTruyen(authorAlias,1);
				if(tacGia == null){
					tacGia = new TacGia(author,authorAlias,1);
					tacGiaService.save(tacGia);
				}
				linkSource=linkSource.replace("script", "");
				translator=translator.replace("script", "");
				truyen = new TruyenTinhCam(name, alias, linkImage, nameImage, info, tacGia, totalChapter, isDelete, isFinished, isHot, liked, views, viewsWeek, viewsMonth, sourceBy, linkSource, translator, new Date(),acc);
				truyenTinhCamService.save(truyen);
				model.addAttribute("notice", "successfully");
			}
			return "redirect:/quan-ly/truyen-tinh-cam/danh-sach.htm";
		}else{
			model.addAttribute("notice", "error");
			return "redirect:/quan-ly/truyen-tinh-cam/chinh-sua-truyen.htm";
		}
	}
	
	//xu ly sua truyen
	@RequestMapping("/truyen-tinh-cam/xu-ly-chinh-sua-truyen.htm")
	public String editTruyenTinhCam(Model model,
			@RequestParam(defaultValue="") Long idTruyen,
			@RequestParam(defaultValue="") String name,
			@RequestParam(defaultValue="") String linkImage,
			@RequestParam(defaultValue="") String nameImage,
			@RequestParam(defaultValue="") String info,
			@RequestParam(defaultValue="") String author,
			@RequestParam(defaultValue="") String categories,
			@RequestParam(defaultValue="") float totalChapter,
			@RequestParam(defaultValue="") boolean isDelete,
			@RequestParam(defaultValue="") boolean isFinished,
			@RequestParam(defaultValue="") boolean isHot,
			@RequestParam(defaultValue="") long liked,
			@RequestParam(defaultValue="") long views,
			@RequestParam(defaultValue="") long viewsWeek,
			@RequestParam(defaultValue="") long viewsMonth,
			@RequestParam(defaultValue="") int sourceBy,
			@RequestParam(defaultValue="") String linkSource,
			@RequestParam(defaultValue="") String translator){
		if(idTruyen != null && idTruyen > 0 && name != null && !name.equals("")){
			name = name.replace("script", "");
			String alias = Unsigned.boDau(name);
			TruyenTinhCam truyen = truyenTinhCamService.findByProperty("alias", alias);
			if(truyen != null && truyen.getId() != idTruyen){
				linkImage=linkImage.replace("script", "");
				nameImage=nameImage.replace("script", "");
				info=info.replace("script", "");
				author=author.replace("script", "");
				String authorAlias = Unsigned.boDau(author);
				TacGia tacGia = tacGiaService.findByAliasLoaiTruyen(authorAlias,1);
				if(tacGia == null){
					tacGia = new TacGia(author,authorAlias,1);
					tacGiaService.save(tacGia);
				}
				categories=categories.replace("script", "");
				linkSource=linkSource.replace("script", "");
				translator=translator.replace("script", "");
				truyenTinhCamService.update(idTruyen, name, alias, linkImage, nameImage, info, tacGia, categories, totalChapter, isDelete, isFinished, isHot, liked, views, viewsWeek, viewsMonth, sourceBy, linkSource, translator);
				model.addAttribute("notice", "successfully");
			}
		}else{
			model.addAttribute("notice", "error");
			return "redirect:/quan-ly/truyen-tinh-cam/chinh-sua-truyen.htm?id="+idTruyen;
		}
		return "redirect:/quan-ly/truyen-tinh-cam/danh-sach.htm";
	}
	
	//set hot truyen
	@RequestMapping("/truyen-tinh-cam/set-hot.htm")
	public String setTruyenTinhCamHot(@RequestParam Long id){
		if(id != null && id > 0){
			truyenTinhCamService.setHot(id);
		}
		return "redirect:/quan-ly/truyen-tinh-cam/danh-sach.htm";
	}
	
	//set isDelete truyen
	@RequestMapping("/truyen-tinh-cam/set-isDelete-truyen.htm")
	public String setIsDeleteTruyenTinhCam(Model model,@RequestParam Long id){
		if(id != null && id > 0){
			truyenTinhCamService.setIsDelete(id);
			model.addAttribute("notice", "successfully");
		}else{
			model.addAttribute("notice", "error");
		}
		return "redirect:/quan-ly/truyen-tinh-cam/danh-sach.htm";
	}
	
	//xoa truyen
	@RequestMapping("/truyen-tinh-cam/xoa-truyen.htm")
	public String deleteTruyenTinhCam(Model model, @RequestParam Long id){
		if(id != null && id > 0){
			truyenTinhCamService.delete(id);
			model.addAttribute("notice", "successfully");
		}else{
			model.addAttribute("notice", "error");
		}
		return "redirect:/quan-ly/truyen-tinh-cam/danh-sach.htm";
	}
	
	//doi totalChapter
	@RequestMapping("/truyen-tinh-cam/doi-totalChapter.htm")
	public void changeTotalChapterTruyenTinhCam(@RequestParam Long id,
			@RequestParam float totalChapter){
		if(id > 0 && totalChapter > 0){
			truyenTinhCamService.updateTotalChapterr(id,totalChapter);
		}
	}
	
	//set isDelete chapter truyen
	@RequestMapping("/chapterTruyenTinhCam/set-isDelete-chapter-truyen.htm")
	public String setIsDeleteChapterTruyenTinhCam(@RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
		String rs = "redirect:/quan-ly/chapterTruyenTinhCam/danh-sach-tat-ca-chapter-truyen.htm";
		if(id != null && id > 0){
			chapterTruyenTinhCamService.setIsDelete(id);
			if(idTruyen != null && idTruyen > 0){
				rs = "redirect:/quan-ly/chapterTruyenTinhCam/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
			}
		}
		return rs;
	}
	
	//danh sach chapter truyen theo truyen
	@RequestMapping("/chapterTruyenTinhCam/danh-sach-theo-truyen.htm")
	public String danhSachChapterTheoTruyenTinhCam(Model model,
			@RequestParam Long idTruyen,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "chapterNumber") String sortBy,
			@RequestParam(defaultValue = "notNull") String contentIsNull,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		if(idTruyen != null && idTruyen > 0){
			TruyenTinhCam truyen = truyenTinhCamService.findById(idTruyen);
			if(truyen!=null){
				PagingInfo pInfo = new PagingInfo();
				pInfo.setCurrentPage(curPage);
				pInfo.setSizePerPage(sizePerPage);
				List<ChapterTmpTruyenTinhCam> lstChapterTruyen = chapterTmpTruyenTinhCamService
						.getLstChapterByTruyen(truyen, kwSearch, sort, sortBy, pInfo);
				model.addAttribute("idTruyen", idTruyen);
				model.addAttribute("kwSearch", kwSearch);
				model.addAttribute("totalSize", pInfo.getCount());
				model.addAttribute("sizePerPage", sizePerPage);
				model.addAttribute("curPage", curPage);
				model.addAttribute("sort", sort);
				model.addAttribute("sortBy", sortBy);
				model.addAttribute("contentIsNull", contentIsNull);
				model.addAttribute("pInfo", pInfo);
				model.addAttribute("lstChapterTruyen", lstChapterTruyen);
				return "/data/chapterTruyenTinhCam/lstChapterByTruyen";
			}else{
				return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
			}
		}else{
			return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
		}
	}
	
	//danh sach tat ca chapter truyen
	@RequestMapping("/chapterTruyenTinhCam/danh-sach-tat-ca-chapter-truyen.htm")
	public String danhSachChapterTruyenTinhCam(Model model,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		List<ChapterTmpTruyenTinhCam> lstChapterTruyen = chapterTmpTruyenTinhCamService
				.getLstAllChapterTmpTruyenTinhCam(kwSearch, sort, sortBy, pInfo);
		model.addAttribute("kwSearch", kwSearch);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstChapterTruyen", lstChapterTruyen);
		return "/data/chapterTruyenTinhCam/lstAllChapterTruyen";
	}
	
	//doi chapternumber cua chapter truyen
	@RequestMapping("/chapterTruyenTinhCam/doi-chapterNumber.htm")
	public void changeChapterNumberTruyenTinhCam(@RequestParam Long id,
			@RequestParam float chapterNumberChange){
		if(id > 0 && chapterNumberChange > 0){
			chapterTruyenTinhCamService.updateChapterNumber(id,chapterNumberChange);
			chapterTmpTruyenTinhCamService.updateChapterNumber(id,chapterNumberChange);
		}
	}
	
	//chinh sua chapter truyen
	@RequestMapping("/chapterTruyenTinhCam/cap-nhat-chapter-truyen.htm")
	public String editChapterTruyenTinhCam(Model model, @RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
		if(id!=null && id > 0){
			ChapterTruyenTinhCam chapter = chapterTruyenTinhCamService.findById(id);
			model.addAttribute("chapter", chapter);
			if(idTruyen!=null && idTruyen > 0){
				model.addAttribute("idTruyen", idTruyen);
			}
			return "/data/chapterTruyenTinhCam/editChapter";
		}else{
			return "redirect:/quan-ly/truyen-tinh-cam/danh-sach.htm";
		}
	}
	
	//xu ly chinh sua chapter truyen
	@RequestMapping("/chapterTruyenTinhCam/xu-ly-cap-nhat-chapter.htm")
	public String processEditChapterTruyenTinhCam(Model model,
			@RequestParam Long id,
			@RequestParam(defaultValue="") Long idTruyen,
			@RequestParam(defaultValue="") String name,
			@RequestParam(defaultValue="") Float chapterNumber,
			@RequestParam(defaultValue="") String content,
			@RequestParam(defaultValue="") boolean isDelete,
			@RequestParam(defaultValue="") boolean isHot) {
		if(id!=null && id > 0 && chapterNumber!=null && chapterNumber > 0 && content!=null && !content.equals("")){
			ChapterTruyenTinhCam chapter = chapterTruyenTinhCamService.findById(id);
			if(chapter != null){
				chapterTmpTruyenTinhCamService.update(id, chapterNumber);
				chapterTruyenTinhCamService.update(id, chapterNumber, content, isDelete, isHot);
				model.addAttribute("notice", "successfully");
			}else{
				model.addAttribute("notice", "error");
			}
		}else{
			model.addAttribute("notice", "error");
		}
		if(idTruyen != null){
			return "redirect:/quan-ly/chapterTruyenTinhCam/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
		}else{
			return "redirect:/quan-ly/chapterTruyenTinhCam/danh-sach-tat-ca-chapter-truyen.htm";
		}
	}
	
	//cap nhat chapternumber theo chaptertruyen
	@RequestMapping("/chapterTruyenTinhCam/cap-nhat-lai-chapter-number.htm")
	public String upadteLsChapterNumbertChapterTruyenTinhCam() {
		chapterTruyenTinhCamService.updateLstChapterNumberBlogTruyen();
		return "redirect:/quan-ly/truyen-tinh-cam/danh-sach.htm";
	}
	
	/****************************************************
	 * 
	 * Truyen Trinh Tham
	 * 
	 ****************************************************/
	
	//danh sach truyen
	@RequestMapping("/truyen-trinh-tham/danh-sach.htm")
	public String danhSachTruyenTrinhTham(Model model,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		List<TruyenTrinhTham> lstTruyen = truyenTrinhThamService
				.getAllLst(kwSearch, sort, sortBy, pInfo);
		model.addAttribute("kwSearch", kwSearch);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstTruyen", lstTruyen);
		return "/data/truyenTrinhTham/lstTruyen";
	}
	
	//chinh sua - them truyen
	@RequestMapping("/truyen-trinh-tham/chinh-sua-truyen.htm")
	public String chinhSuaTruyenTrinhTham(Model model,@RequestParam(defaultValue="") Long id){
		if(id != null && id > 0){
			TruyenTrinhTham truyen = truyenTrinhThamService.findById(id);
			if(truyen==null){
				return "redirect:/quan-ly/truyen-trinh-tham/danh-sach.htm";
			}
			model.addAttribute("truyen", truyen);
		}
		return "/data/truyenTrinhTham/addEditTruyen";
	}
	
	//xu ly them truyen
	@RequestMapping("/truyen-trinh-tham/xu-ly-them-truyen.htm")
	public String addTruyenTrinhTham(Model model,
			@RequestParam(defaultValue="") Long idAccount,
			@RequestParam(defaultValue="") String name,
			@RequestParam(defaultValue="") String linkImage,
			@RequestParam(defaultValue="") String nameImage,
			@RequestParam(defaultValue="") String info,
			@RequestParam(defaultValue="") String author,
			@RequestParam(defaultValue="") float totalChapter,
			@RequestParam(defaultValue="") boolean isDelete,
			@RequestParam(defaultValue="") boolean isFinished,
			@RequestParam(defaultValue="") boolean isHot,
			@RequestParam(defaultValue="") long liked,
			@RequestParam(defaultValue="") long views,
			@RequestParam(defaultValue="") long viewsWeek,
			@RequestParam(defaultValue="") long viewsMonth,
			@RequestParam(defaultValue="") int sourceBy,
			@RequestParam(defaultValue="") String linkSource,
			@RequestParam(defaultValue="") String translator){
		if(idAccount!=null && idAccount > 0 && name != null && !name.equals("")){
			name = name.replace("script", "");
			String alias = Unsigned.boDau(name);
			TruyenTrinhTham truyen = truyenTrinhThamService.findByProperty("alias", alias);
			if(truyen == null){
				Account acc = userService.findById(idAccount);
				linkImage=linkImage.replace("script", "");
				nameImage=nameImage.replace("script", "");
				info=info.replace("script", "");
				author=author.replace("script", "");
				String authorAlias = Unsigned.boDau(author);
				TacGia tacGia = tacGiaService.findByAliasLoaiTruyen(authorAlias,1);
				if(tacGia == null){
					tacGia = new TacGia(author,authorAlias,1);
					tacGiaService.save(tacGia);
				}
				linkSource=linkSource.replace("script", "");
				translator=translator.replace("script", "");
				truyen = new TruyenTrinhTham(name, alias, linkImage, nameImage, info, tacGia, totalChapter, isDelete, isFinished, isHot, liked, views, viewsWeek, viewsMonth, sourceBy, linkSource, translator, new Date(),acc);
				truyenTrinhThamService.save(truyen);
				model.addAttribute("notice", "successfully");
			}
			return "redirect:/quan-ly/truyen-trinh-tham/danh-sach.htm";
		}else{
			model.addAttribute("notice", "error");
			return "redirect:/quan-ly/truyen-trinh-tham/chinh-sua-truyen.htm";
		}
	}
	
	//xu ly sua truyen
	@RequestMapping("/truyen-trinh-tham/xu-ly-chinh-sua-truyen.htm")
	public String editTruyenTrinhTham(Model model,
			@RequestParam(defaultValue="") Long idTruyen,
			@RequestParam(defaultValue="") String name,
			@RequestParam(defaultValue="") String linkImage,
			@RequestParam(defaultValue="") String nameImage,
			@RequestParam(defaultValue="") String info,
			@RequestParam(defaultValue="") String author,
			@RequestParam(defaultValue="") String categories,
			@RequestParam(defaultValue="") float totalChapter,
			@RequestParam(defaultValue="") boolean isDelete,
			@RequestParam(defaultValue="") boolean isFinished,
			@RequestParam(defaultValue="") boolean isHot,
			@RequestParam(defaultValue="") long liked,
			@RequestParam(defaultValue="") long views,
			@RequestParam(defaultValue="") long viewsWeek,
			@RequestParam(defaultValue="") long viewsMonth,
			@RequestParam(defaultValue="") int sourceBy,
			@RequestParam(defaultValue="") String linkSource,
			@RequestParam(defaultValue="") String translator){
		if(idTruyen != null && idTruyen > 0 && name != null && !name.equals("")){
			name = name.replace("script", "");
			String alias = Unsigned.boDau(name);
			TruyenTrinhTham truyen = truyenTrinhThamService.findByProperty("alias", alias);
			if(truyen != null && truyen.getId() != idTruyen){
				linkImage=linkImage.replace("script", "");
				nameImage=nameImage.replace("script", "");
				info=info.replace("script", "");
				author=author.replace("script", "");
				String authorAlias = Unsigned.boDau(author);
				TacGia tacGia = tacGiaService.findByAliasLoaiTruyen(authorAlias,1);
				if(tacGia == null){
					tacGia = new TacGia(author,authorAlias,1);
					tacGiaService.save(tacGia);
				}
				categories=categories.replace("script", "");
				linkSource=linkSource.replace("script", "");
				translator=translator.replace("script", "");
				truyenTrinhThamService.update(idTruyen, name, alias, linkImage, nameImage, info, tacGia, categories, totalChapter, isDelete, isFinished, isHot, liked, views, viewsWeek, viewsMonth, sourceBy, linkSource, translator);
				model.addAttribute("notice", "successfully");
			}
		}else{
			model.addAttribute("notice", "error");
			return "redirect:/quan-ly/truyen-trinh-tham/chinh-sua-truyen.htm?id="+idTruyen;
		}
		return "redirect:/quan-ly/truyen-trinh-tham/danh-sach.htm";
	}
	
	//set hot truyen
	@RequestMapping("/truyen-trinh-tham/set-hot.htm")
	public String setTruyenTrinhThamHot(@RequestParam Long id){
		if(id != null && id > 0){
			truyenTrinhThamService.setHot(id);
		}
		return "redirect:/quan-ly/truyen-trinh-tham/danh-sach.htm";
	}
	
	//set isDelete truyen
	@RequestMapping("/truyen-trinh-tham/set-isDelete-truyen.htm")
	public String setIsDeleteTruyenTrinhTham(Model model,@RequestParam Long id){
		if(id != null && id > 0){
			truyenTrinhThamService.setIsDelete(id);
			model.addAttribute("notice", "successfully");
		}else{
			model.addAttribute("notice", "error");
		}
		return "redirect:/quan-ly/truyen-trinh-tham/danh-sach.htm";
	}
	
	//xoa truyen
	@RequestMapping("/truyen-trinh-tham/xoa-truyen.htm")
	public String deleteTruyenTrinhTham(Model model, @RequestParam Long id){
		if(id != null && id > 0){
			truyenTrinhThamService.delete(id);
			model.addAttribute("notice", "successfully");
		}else{
			model.addAttribute("notice", "error");
		}
		return "redirect:/quan-ly/truyen-trinh-tham/danh-sach.htm";
	}
	
	//doi totalChapter
	@RequestMapping("/truyen-trinh-tham/doi-totalChapter.htm")
	public void changeTotalChapterTruyenTrinhTham(@RequestParam Long id,
			@RequestParam float totalChapter){
		if(id > 0 && totalChapter > 0){
			truyenTrinhThamService.updateTotalChapterr(id,totalChapter);
		}
	}
	
	//set isDelete chapter truyen
	@RequestMapping("/chapterTruyenTrinhTham/set-isDelete-chapter-truyen.htm")
	public String setIsDeleteChapterTruyenTrinhTham(@RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
		String rs = "redirect:/quan-ly/chapterTruyenTrinhTham/danh-sach-tat-ca-chapter-truyen.htm";
		if(id != null && id > 0){
			chapterTruyenTrinhThamService.setIsDelete(id);
			if(idTruyen != null && idTruyen > 0){
				rs = "redirect:/quan-ly/chapterTruyenTrinhTham/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
			}
		}
		return rs;
	}
	
	//danh sach chapter truyen theo truyen
	@RequestMapping("/chapterTruyenTrinhTham/danh-sach-theo-truyen.htm")
	public String danhSachChapterTheoTruyenTrinhTham(Model model,
			@RequestParam Long idTruyen,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "chapterNumber") String sortBy,
			@RequestParam(defaultValue = "notNull") String contentIsNull,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		if(idTruyen != null && idTruyen > 0){
			TruyenTrinhTham truyen = truyenTrinhThamService.findById(idTruyen);
			if(truyen!=null){
				PagingInfo pInfo = new PagingInfo();
				pInfo.setCurrentPage(curPage);
				pInfo.setSizePerPage(sizePerPage);
				List<ChapterTmpTruyenTrinhTham> lstChapterTruyen = chapterTmpTruyenTrinhThamService
						.getLstChapterByTruyen(truyen, kwSearch, sort, sortBy, pInfo);
				model.addAttribute("idTruyen", idTruyen);
				model.addAttribute("kwSearch", kwSearch);
				model.addAttribute("totalSize", pInfo.getCount());
				model.addAttribute("sizePerPage", sizePerPage);
				model.addAttribute("curPage", curPage);
				model.addAttribute("sort", sort);
				model.addAttribute("sortBy", sortBy);
				model.addAttribute("contentIsNull", contentIsNull);
				model.addAttribute("pInfo", pInfo);
				model.addAttribute("lstChapterTruyen", lstChapterTruyen);
				return "/data/chapterTruyenTrinhTham/lstChapterByTruyen";
			}else{
				return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
			}
		}else{
			return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
		}
	}
	
	//danh sach tat ca chapter truyen
	@RequestMapping("/chapterTruyenTrinhTham/danh-sach-tat-ca-chapter-truyen.htm")
	public String danhSachChapterTruyenTrinhTham(Model model,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		List<ChapterTmpTruyenTrinhTham> lstChapterTruyen = chapterTmpTruyenTrinhThamService
				.getLstAllChapterTmpTruyenTrinhTham(kwSearch, sort, sortBy, pInfo);
		model.addAttribute("kwSearch", kwSearch);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstChapterTruyen", lstChapterTruyen);
		return "/data/chapterTruyenTrinhTham/lstAllChapterTruyen";
	}
	
	//doi chapternumber cua chapter truyen
	@RequestMapping("/chapterTruyenTrinhTham/doi-chapterNumber.htm")
	public void changeChapterNumberTruyenTrinhTham(@RequestParam Long id,
			@RequestParam float chapterNumberChange){
		if(id > 0 && chapterNumberChange > 0){
			chapterTruyenTrinhThamService.updateChapterNumber(id,chapterNumberChange);
			chapterTmpTruyenTrinhThamService.updateChapterNumber(id,chapterNumberChange);
		}
	}
	
	//chinh sua chapter truyen
	@RequestMapping("/chapterTruyenTrinhTham/cap-nhat-chapter-truyen.htm")
	public String editChapterTruyenTrinhTham(Model model, @RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
		if(id!=null && id > 0){
			ChapterTruyenTrinhTham chapter = chapterTruyenTrinhThamService.findById(id);
			model.addAttribute("chapter", chapter);
			if(idTruyen!=null && idTruyen > 0){
				model.addAttribute("idTruyen", idTruyen);
			}
			return "/data/chapterTruyenTrinhTham/editChapter";
		}else{
			return "redirect:/quan-ly/truyen-trinh-tham/danh-sach.htm";
		}
	}
	
	//xu ly chinh sua chapter truyen
	@RequestMapping("/chapterTruyenTrinhTham/xu-ly-cap-nhat-chapter.htm")
	public String processEditChapterTruyenTrinhTham(Model model,
			@RequestParam Long id,
			@RequestParam(defaultValue="") Long idTruyen,
			@RequestParam(defaultValue="") String name,
			@RequestParam(defaultValue="") Float chapterNumber,
			@RequestParam(defaultValue="") String content,
			@RequestParam(defaultValue="") boolean isDelete,
			@RequestParam(defaultValue="") boolean isHot) {
		if(id!=null && id > 0 && chapterNumber!=null && chapterNumber > 0 && content!=null && !content.equals("")){
			ChapterTruyenTrinhTham chapter = chapterTruyenTrinhThamService.findById(id);
			if(chapter != null){
				chapterTmpTruyenTrinhThamService.update(id, chapterNumber);
				chapterTruyenTrinhThamService.update(id, chapterNumber, content, isDelete, isHot);
				model.addAttribute("notice", "successfully");
			}else{
				model.addAttribute("notice", "error");
			}
		}else{
			model.addAttribute("notice", "error");
		}
		if(idTruyen != null){
			return "redirect:/quan-ly/chapterTruyenTrinhTham/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
		}else{
			return "redirect:/quan-ly/chapterTruyenTrinhTham/danh-sach-tat-ca-chapter-truyen.htm";
		}
	}
	
	//cap nhat chapternumber theo chaptertruyen
	@RequestMapping("/chapterTruyenTrinhTham/cap-nhat-lai-chapter-number.htm")
	public String upadteLsChapterNumbertChapterTruyenTrinhTham() {
		chapterTruyenTrinhThamService.updateLstChapterNumberBlogTruyen();
		return "redirect:/quan-ly/truyen-trinh-tham/danh-sach.htm";
	}
	
	/****************************************************
	 * 
	 * Truyen ma
	 * 
	 ****************************************************/
	  //danh sach truyen
	  @RequestMapping("/truyen-ma/danh-sach.htm")
	  public String danhSachTruyenMa(Model model,
	      @RequestParam(defaultValue = "") String kwSearch,
	      @RequestParam(defaultValue = "desc") String sort,
	      @RequestParam(defaultValue = "id") String sortBy,
	      @RequestParam(defaultValue = "1") int curPage,
	      @RequestParam(defaultValue = "10") int sizePerPage) {
	    PagingInfo pInfo = new PagingInfo();
	    pInfo.setCurrentPage(curPage);
	    pInfo.setSizePerPage(sizePerPage);
	    List<TruyenMa> lstTruyen = truyenMaService
	        .getAllLst(kwSearch, sort, sortBy, pInfo);
	    model.addAttribute("kwSearch", kwSearch);
	    model.addAttribute("totalSize", pInfo.getCount());
	    model.addAttribute("sizePerPage", sizePerPage);
	    model.addAttribute("curPage", curPage);
	    model.addAttribute("sort", sort);
	    model.addAttribute("sortBy", sortBy);
	    model.addAttribute("pInfo", pInfo);
	    model.addAttribute("lstTruyen", lstTruyen);
	    return "/data/truyenMa/lstTruyen";
	  }
	  
	  //chinh sua - them truyen
	  @RequestMapping("/truyen-ma/chinh-sua-truyen.htm")
	  public String chinhSuaTruyenMa(Model model,@RequestParam(defaultValue="") Long id){
	    if(id != null && id > 0){
	      TruyenMa truyen = truyenMaService.findById(id);
	      if(truyen==null){
	        return "redirect:/quan-ly/truyen-ma/danh-sach.htm";
	      }
	      model.addAttribute("truyen", truyen);
	    }
	    return "/data/truyenMa/addEditTruyen";
	  }
	  
	  //xu ly them truyen
	  @RequestMapping("/truyen-ma/xu-ly-them-truyen.htm")
	  public String addTruyenMa(Model model,
	      @RequestParam(defaultValue="") Long idAccount,
	      @RequestParam(defaultValue="") String name,
	      @RequestParam(defaultValue="") String linkImage,
	      @RequestParam(defaultValue="") String nameImage,
	      @RequestParam(defaultValue="") String info,
	      @RequestParam(defaultValue="") String author,
	      @RequestParam(defaultValue="") float totalChapter,
	      @RequestParam(defaultValue="") boolean isDelete,
	      @RequestParam(defaultValue="") boolean isFinished,
	      @RequestParam(defaultValue="") boolean isHot,
	      @RequestParam(defaultValue="") long liked,
	      @RequestParam(defaultValue="") long views,
	      @RequestParam(defaultValue="") long viewsWeek,
	      @RequestParam(defaultValue="") long viewsMonth,
	      @RequestParam(defaultValue="") int sourceBy,
	      @RequestParam(defaultValue="") String linkSource,
	      @RequestParam(defaultValue="") String translator){
	    if(idAccount!=null && idAccount > 0 && name != null && !name.equals("")){
	      name = name.replace("script", "");
	      String alias = Unsigned.boDau(name);
	      TruyenMa truyen = truyenMaService.findByProperty("alias", alias);
	      if(truyen == null){
	        Account acc = userService.findById(idAccount);
	        linkImage=linkImage.replace("script", "");
	        nameImage=nameImage.replace("script", "");
	        info=info.replace("script", "");
	        author=author.replace("script", "");
	        String authorAlias = Unsigned.boDau(author);
	        TacGia tacGia = tacGiaService.findByAliasLoaiTruyen(authorAlias,1);
	        if(tacGia == null){
	          tacGia = new TacGia(author,authorAlias,1);
	          tacGiaService.save(tacGia);
	        }
	        linkSource=linkSource.replace("script", "");
	        translator=translator.replace("script", "");
	        truyen = new TruyenMa(name, alias, linkImage, nameImage, info, tacGia, totalChapter, isDelete, isFinished, isHot, liked, views, viewsWeek, viewsMonth, sourceBy, linkSource, translator, new Date(),acc);
	        truyenMaService.save(truyen);
	        model.addAttribute("notice", "successfully");
	      }
	      return "redirect:/quan-ly/truyen-ma/danh-sach.htm";
	    }else{
	      model.addAttribute("notice", "error");
	      return "redirect:/quan-ly/truyen-ma/chinh-sua-truyen.htm";
	    }
	  }
	  
	  //xu ly sua truyen
	  @RequestMapping("/truyen-ma/xu-ly-chinh-sua-truyen.htm")
	  public String editTruyenMa(Model model,
	      @RequestParam(defaultValue="") Long idTruyen,
	      @RequestParam(defaultValue="") String name,
	      @RequestParam(defaultValue="") String linkImage,
	      @RequestParam(defaultValue="") String nameImage,
	      @RequestParam(defaultValue="") String info,
	      @RequestParam(defaultValue="") String author,
	      @RequestParam(defaultValue="") String categories,
	      @RequestParam(defaultValue="") float totalChapter,
	      @RequestParam(defaultValue="") boolean isDelete,
	      @RequestParam(defaultValue="") boolean isFinished,
	      @RequestParam(defaultValue="") boolean isHot,
	      @RequestParam(defaultValue="") long liked,
	      @RequestParam(defaultValue="") long views,
	      @RequestParam(defaultValue="") long viewsWeek,
	      @RequestParam(defaultValue="") long viewsMonth,
	      @RequestParam(defaultValue="") int sourceBy,
	      @RequestParam(defaultValue="") String linkSource,
	      @RequestParam(defaultValue="") String translator){
	    if(idTruyen != null && idTruyen > 0 && name != null && !name.equals("")){
	      name = name.replace("script", "");
	      String alias = Unsigned.boDau(name);
	      TruyenMa truyen = truyenMaService.findByProperty("alias", alias);
	      if(truyen != null && truyen.getId() != idTruyen){
	        linkImage=linkImage.replace("script", "");
	        nameImage=nameImage.replace("script", "");
	        info=info.replace("script", "");
	        author=author.replace("script", "");
	        String authorAlias = Unsigned.boDau(author);
	        TacGia tacGia = tacGiaService.findByAliasLoaiTruyen(authorAlias,1);
	        if(tacGia == null){
	          tacGia = new TacGia(author,authorAlias,1);
	          tacGiaService.save(tacGia);
	        }
	        categories=categories.replace("script", "");
	        linkSource=linkSource.replace("script", "");
	        translator=translator.replace("script", "");
	        truyenMaService.update(idTruyen, name, alias, linkImage, nameImage, info, tacGia, categories, totalChapter, isDelete, isFinished, isHot, liked, views, viewsWeek, viewsMonth, sourceBy, linkSource, translator);
	        model.addAttribute("notice", "successfully");
	      }
	    }else{
	      model.addAttribute("notice", "error");
	      return "redirect:/quan-ly/truyen-ma/chinh-sua-truyen.htm?id="+idTruyen;
	    }
	    return "redirect:/quan-ly/truyen-ma/danh-sach.htm";
	  }
	  
	  //set hot truyen
	  @RequestMapping("/truyen-ma/set-hot.htm")
	  public String setTruyenMaHot(@RequestParam Long id){
	    if(id != null && id > 0){
	      truyenMaService.setHot(id);
	    }
	    return "redirect:/quan-ly/truyen-ma/danh-sach.htm";
	  }
	  
	  //set isDelete truyen
	  @RequestMapping("/truyen-ma/set-isDelete-truyen.htm")
	  public String setIsDeleteTruyenMa(Model model,@RequestParam Long id){
	    if(id != null && id > 0){
	      truyenMaService.setIsDelete(id);
	      model.addAttribute("notice", "successfully");
	    }else{
	      model.addAttribute("notice", "error");
	    }
	    return "redirect:/quan-ly/truyen-ma/danh-sach.htm";
	  }
	  
	  //xoa truyen
	  @RequestMapping("/truyen-ma/xoa-truyen.htm")
	  public String deleteTruyenMa(Model model, @RequestParam Long id){
	    if(id != null && id > 0){
	      truyenMaService.delete(id);
	      model.addAttribute("notice", "successfully");
	    }else{
	      model.addAttribute("notice", "error");
	    }
	    return "redirect:/quan-ly/truyen-ma/danh-sach.htm";
	  }
	  
	  //doi totalChapter
	  @RequestMapping("/truyen-ma/doi-totalChapter.htm")
	  public void changeTotalChapterTruyenMa(@RequestParam Long id,
	      @RequestParam float totalChapter){
	    if(id > 0 && totalChapter > 0){
	      truyenMaService.updateTotalChapterr(id,totalChapter);
	    }
	  }
	  
	  //set isDelete chapter truyen
	  @RequestMapping("/chapterTruyenMa/set-isDelete-chapter-truyen.htm")
	  public String setIsDeleteChapterTruyenMa(@RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
	    String rs = "redirect:/quan-ly/chapterTruyenMa/danh-sach-tat-ca-chapter-truyen.htm";
	    if(id != null && id > 0){
	      chapterTruyenMaService.setIsDelete(id);
	      if(idTruyen != null && idTruyen > 0){
	        rs = "redirect:/quan-ly/chapterTruyenMa/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
	      }
	    }
	    return rs;
	  }
	  
	  //danh sach chapter truyen theo truyen
	  @RequestMapping("/chapterTruyenMa/danh-sach-theo-truyen.htm")
	  public String danhSachChapterTheoTruyenMa(Model model,
	      @RequestParam Long idTruyen,
	      @RequestParam(defaultValue = "") String kwSearch,
	      @RequestParam(defaultValue = "desc") String sort,
	      @RequestParam(defaultValue = "chapterNumber") String sortBy,
	      @RequestParam(defaultValue = "notNull") String contentIsNull,
	      @RequestParam(defaultValue = "1") int curPage,
	      @RequestParam(defaultValue = "10") int sizePerPage) {
	    if(idTruyen != null && idTruyen > 0){
	      TruyenMa truyen = truyenMaService.findById(idTruyen);
	      if(truyen!=null){
	        PagingInfo pInfo = new PagingInfo();
	        pInfo.setCurrentPage(curPage);
	        pInfo.setSizePerPage(sizePerPage);
	        List<ChapterTmpTruyenMa> lstChapterTruyen = chapterTmpTruyenMaService
	            .getLstChapterByTruyen(truyen, kwSearch, sort, sortBy, pInfo);
	        model.addAttribute("idTruyen", idTruyen);
	        model.addAttribute("kwSearch", kwSearch);
	        model.addAttribute("totalSize", pInfo.getCount());
	        model.addAttribute("sizePerPage", sizePerPage);
	        model.addAttribute("curPage", curPage);
	        model.addAttribute("sort", sort);
	        model.addAttribute("sortBy", sortBy);
	        model.addAttribute("contentIsNull", contentIsNull);
	        model.addAttribute("pInfo", pInfo);
	        model.addAttribute("lstChapterTruyen", lstChapterTruyen);
	        return "/data/chapterTruyenMa/lstChapterByTruyen";
	      }else{
	        return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
	      }
	    }else{
	      return "redirect:/quan-ly/tai-khoan/tao-tai-khoan.htm";
	    }
	  }
	  
	  //danh sach tat ca chapter truyen
	  @RequestMapping("/chapterTruyenMa/danh-sach-tat-ca-chapter-truyen.htm")
	  public String danhSachChapterTruyenMa(Model model,
	      @RequestParam(defaultValue = "") String kwSearch,
	      @RequestParam(defaultValue = "desc") String sort,
	      @RequestParam(defaultValue = "id") String sortBy,
	      @RequestParam(defaultValue = "1") int curPage,
	      @RequestParam(defaultValue = "10") int sizePerPage) {
	    PagingInfo pInfo = new PagingInfo();
	    pInfo.setCurrentPage(curPage);
	    pInfo.setSizePerPage(sizePerPage);
	    List<ChapterTmpTruyenMa> lstChapterTruyen = chapterTmpTruyenMaService
	        .getLstAllChapterTmpTruyenMa(kwSearch, sort, sortBy, pInfo);
	    model.addAttribute("kwSearch", kwSearch);
	    model.addAttribute("totalSize", pInfo.getCount());
	    model.addAttribute("sizePerPage", sizePerPage);
	    model.addAttribute("curPage", curPage);
	    model.addAttribute("sort", sort);
	    model.addAttribute("sortBy", sortBy);
	    model.addAttribute("pInfo", pInfo);
	    model.addAttribute("lstChapterTruyen", lstChapterTruyen);
	    return "/data/chapterTruyenMa/lstAllChapterTruyen";
	  }
	  
	  //doi chapternumber cua chapter truyen
	  @RequestMapping("/chapterTruyenMa/doi-chapterNumber.htm")
	  public void changeChapterNumberTruyenMa(@RequestParam Long id,
	      @RequestParam float chapterNumberChange){
	    if(id > 0 && chapterNumberChange > 0){
	      chapterTruyenMaService.updateChapterNumber(id,chapterNumberChange);
	      chapterTmpTruyenMaService.updateChapterNumber(id,chapterNumberChange);
	    }
	  }
	  
	  //chinh sua chapter truyen
	  @RequestMapping("/chapterTruyenMa/cap-nhat-chapter-truyen.htm")
	  public String editChapterTruyenMa(Model model, @RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
	    if(id!=null && id > 0){
	      ChapterTruyenMa chapter = chapterTruyenMaService.findById(id);
	      model.addAttribute("chapter", chapter);
	      if(idTruyen!=null && idTruyen > 0){
	        model.addAttribute("idTruyen", idTruyen);
	      }
	      return "/data/chapterTruyenMa/editChapter";
	    }else{
	      return "redirect:/quan-ly/truyen-ma/danh-sach.htm";
	    }
	  }
	  
	  //xu ly chinh sua chapter truyen
	  @RequestMapping("/chapterTruyenMa/xu-ly-cap-nhat-chapter.htm")
	  public String processEditChapterTruyenMa(Model model,
	      @RequestParam Long id,
	      @RequestParam(defaultValue="") Long idTruyen,
	      @RequestParam(defaultValue="") String name,
	      @RequestParam(defaultValue="") Float chapterNumber,
	      @RequestParam(defaultValue="") String content,
	      @RequestParam(defaultValue="") boolean isDelete,
	      @RequestParam(defaultValue="") boolean isHot) {
	    if(id!=null && id > 0 && chapterNumber!=null && chapterNumber > 0 && content!=null && !content.equals("")){
	      ChapterTruyenMa chapter = chapterTruyenMaService.findById(id);
	      if(chapter != null){
	        chapterTmpTruyenMaService.update(id, chapterNumber);
	        chapterTruyenMaService.update(id, chapterNumber, content, isDelete, isHot);
	        model.addAttribute("notice", "successfully");
	      }else{
	        model.addAttribute("notice", "error");
	      }
	    }else{
	      model.addAttribute("notice", "error");
	    }
	    if(idTruyen != null){
	      return "redirect:/quan-ly/chapterTruyenMa/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
	    }else{
	      return "redirect:/quan-ly/chapterTruyenMa/danh-sach-tat-ca-chapter-truyen.htm";
	    }
	  }
	  
	  //cap nhat chapternumber theo chaptertruyen
	  @RequestMapping("/chapterTruyenMa/cap-nhat-lai-chapter-number.htm")
	  public String upadteLsChapterNumbertChapterTruyenMa() {
	    chapterTruyenMaService.updateLstChapterNumberBlogTruyen();
	    return "redirect:/quan-ly/truyen-ma/danh-sach.htm";
	  }
	
	/***************************************************
	 * 
	 * Anh dep
	 * 
	 ***************************************************/
	
	//danh sach truyen
	@RequestMapping("/anh-dep/danh-sach.htm")
	public String danhSachAnhDep(Model model,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		List<AnhDep> lstItem = anhDepService
				.getAllLst(kwSearch, sort, sortBy, pInfo);
		model.addAttribute("kwSearch", kwSearch);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstItem", lstItem);
		return "/data/anhDep/lstAnhDep";
	}
	
	//set hot truyen
	@RequestMapping("/anh-dep/set-hot.htm")
	public String setAnhDepHot(@RequestParam Long id){
		if(id != null && id > 0){
			anhDepService.setHot(id);
		}
		return "redirect:/quan-ly/anh-dep/danh-sach.htm";
	}
	
	//set isDelete truyen
	@RequestMapping("/anh-dep/set-isDelete-anh-dep.htm")
	public String setIsDeleteAnhDep(Model model,@RequestParam Long id){
		if(id != null && id > 0){
			anhDepService.setIsDelete(id);
			model.addAttribute("notice", "successfully");
		}else{
			model.addAttribute("notice", "error");
		}
		return "redirect:/quan-ly/anh-dep/danh-sach.htm";
	}
	
	//xoa truyen
	@RequestMapping("/anh-dep/xoa-anh-dep.htm")
	public String deleteTruyenAnhDep(Model model, @RequestParam Long id){
		if(id != null && id > 0){
			anhDepService.delete(id);
			model.addAttribute("notice", "successfully");
		}else{
			model.addAttribute("notice", "error");
		}
		return "redirect:/quan-ly/anh-dep/danh-sach.htm";
	}
	
	//danh sach content anh dep theo anh dep
	@RequestMapping("/content-anh-dep/danh-sach-theo-anh-dep.htm")
	public String danhSachContentAnhDep(Model model,
			@RequestParam Long idAnhDep,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage) {
		if(idAnhDep != null && idAnhDep > 0){
			AnhDep anhDep = anhDepService.findById(idAnhDep);
			if(anhDep!=null){
				PagingInfo pInfo = new PagingInfo();
				pInfo.setCurrentPage(curPage);
				pInfo.setSizePerPage(sizePerPage);
				List<ContentAnhDep> lstItem = contentAnhDepService
						.getLstByAnhDep(anhDep, kwSearch, sort, sortBy, pInfo);
				model.addAttribute("idAnhDep", idAnhDep);
				model.addAttribute("kwSearch", kwSearch);
				model.addAttribute("totalSize", pInfo.getCount());
				model.addAttribute("sizePerPage", sizePerPage);
				model.addAttribute("curPage", curPage);
				model.addAttribute("sort", sort);
				model.addAttribute("sortBy", sortBy);
				model.addAttribute("pInfo", pInfo);
				model.addAttribute("lstItem", lstItem);
				return "/data/contentAnhDep/lstContentAnhDep";
			}else{
				return "redirect:/quan-ly/anh-dep/danh-sach.htm";
			}
		}else{
			return "redirect:/quan-ly/anh-dep/danh-sach.htm";
		}
	}
	
	//danh sach tat ca content anh dep
	@RequestMapping("/content-anh-dep/danh-sach-tat-ca-content-anh-dep.htm")
	public String danhSachContentAnhDep(Model model,
			@RequestParam(defaultValue = "") String kwSearch,
			@RequestParam(defaultValue = "desc") String sort,
			@RequestParam(defaultValue = "id") String sortBy,
			@RequestParam(defaultValue = "1") int curPage,
			@RequestParam(defaultValue = "10") int sizePerPage){
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		pInfo.setSizePerPage(sizePerPage);
		List<ContentAnhDep> lstItem = contentAnhDepService
				.getAllLst(kwSearch, sort, sortBy, pInfo);
		model.addAttribute("kwSearch", kwSearch);
		model.addAttribute("totalSize", pInfo.getCount());
		model.addAttribute("sizePerPage", sizePerPage);
		model.addAttribute("curPage", curPage);
		model.addAttribute("sort", sort);
		model.addAttribute("sortBy", sortBy);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("lstItem", lstItem);
		return "/data/contentAnhDep/lstAllContentAnhDep";
	}
	
	/***************************************************
	 * 
	 * Picasaweb
	 * 
	 ***************************************************/
	
	//danh sach album picasa
	@RequestMapping("/picasaweb/danh-sach-album.htm")
	public String danhSachAlbumPicasaweb(Model model,
			@RequestParam(defaultValue = "anhsexytruyenzcom@gmail.com") String gmail,
			@RequestParam(defaultValue = "11062010") String pwd){
		try {
			List<AlbumEntry> lstItem = PicasawebAlbumUtil.getLstAlbum(gmail, pwd);
			if(lstItem.size()> 0){
				model.addAttribute("totalSize", lstItem.size());
			}
			model.addAttribute("lstItem", lstItem);
			model.addAttribute("gmail", gmail);
			model.addAttribute("pwd", pwd);
			return "/data/picasaweb/lstAlbum";
		} catch (Exception e) {
			return "redirect:/quan-ly/anh-dep/danh-sach.htm";
		}
	}
	
	//danh sach album picasa
	@RequestMapping("/picasaweb/xoa-album.htm")
	public String xoaAlbumPicasaweb(Model model,
			@RequestParam String urlAlbum,
			@RequestParam(defaultValue = "anhsexytruyenzcom") String gmail,
			@RequestParam(defaultValue = "11062010") String pwd){
		if(urlAlbum != null){
			try {
				PicasawebService myService = new PicasawebService("zaza");
				myService.setUserCredentials(gmail, pwd);
				PicasawebAlbumUtil.delAlbum(myService, urlAlbum);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "redirect:/quan-ly/picasaweb/danh-sach-album.htm?gmail="+gmail+"&pwd="+pwd;
	}
	
	//xoa chapter truyen tranh
	@RequestMapping("/chapterTruyenTranh/xoa-chapter-truyen.htm")
	public String deleteChapterTruyenTranh(@RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
		String rs = "redirect:/quan-ly/chapterTruyenTranh/danh-sach-tat-ca-chapter-truyen.htm";
		if(id != null && id > 0){
			chapterTmpTruyenTranhService.delete(id);
			chapterTruyenTranhService.delete(id);
			if(idTruyen != null && idTruyen > 0){
				rs = "redirect:/quan-ly/chapterTruyenTranh/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
			}
		}
		return rs;
	}
	
	//xoa chapter truyen kiem hiep
	@RequestMapping("/chapterTruyenKiemHiep/xoa-chapter-truyen.htm")
	public String deleteChapterTruyenKiemHiep(@RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
		String rs = "redirect:/quan-ly/chapterTruyenKiemHiep/danh-sach-tat-ca-chapter-truyen.htm";
		if(id != null && id > 0){
			chapterTmpTruyenKiemHiepService.delete(id);
			chapterTruyenKiemHiepService.delete(id);
			if(idTruyen != null && idTruyen > 0){
				rs = "redirect:/quan-ly/chapterTruyenKiemHiep/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
			}
		}
		return rs;
	}
	
	//xoa chapter truyen tinh cam
	@RequestMapping("/chapterTruyenTinhCam/xoa-chapter-truyen.htm")
	public String deleteChapterTruyenTinhCam(@RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
		String rs = "redirect:/quan-ly/chapterTruyenTinhCam/danh-sach-tat-ca-chapter-truyen.htm";
		if(id != null && id > 0){
			chapterTmpTruyenTinhCamService.delete(id);
			chapterTruyenTinhCamService.delete(id);
			if(idTruyen != null && idTruyen > 0){
				rs = "redirect:/quan-ly/chapterTruyenTinhCam/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
			}
		}
		return rs;
	}
	
	//xoa chapter truyen trinh tham
	@RequestMapping("/chapterTruyenTrinhTham/xoa-chapter-truyen.htm")
	public String deleteChapterTruyenTrinhTham(@RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
		String rs = "redirect:/quan-ly/chapterTruyenTrinhTham/danh-sach-tat-ca-chapter-truyen.htm";
		if(id != null && id > 0){
			chapterTmpTruyenTrinhThamService.delete(id);
			chapterTruyenTrinhThamService.delete(id);
			if(idTruyen != null && idTruyen > 0){
				rs = "redirect:/quan-ly/chapterTruyenTrinhTham/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
			}
		}
		return rs;
	}
	
	//xoa chapter truyen tranh
	@RequestMapping("/chapterTruyenMa/xoa-chapter-truyen.htm")
	public String deleteChapterTruyenMa(@RequestParam Long id,@RequestParam(defaultValue="") Long idTruyen){
		String rs = "redirect:/quan-ly/chapterTruyenMa/danh-sach-tat-ca-chapter-truyen.htm";
		if(id != null && id > 0){
			chapterTmpTruyenMaService.delete(id);
			chapterTruyenMaService.delete(id);
			if(idTruyen != null && idTruyen > 0){
				rs = "redirect:/quan-ly/chapterTruyenMa/danh-sach-theo-truyen.htm?idTruyen="+idTruyen;
			}
		}
		return rs;
	}
	
	/*******************************************
	 * 
	 * replace thegioima.com thanh truyenz.com
	 * 
	 *******************************************/
	
	//xoa chapter truyen trinh tham
	@RequestMapping("/chapterTruyenMa/replace-thegioima.htm")
	public String replaceTGM(){
		String rs = "redirect:/quan-ly/chapterTruyenMa/danh-sach-tat-ca-chapter-truyen.htm";
		List<ChapterTruyenMa> lstChapterTruyenMa = chapterTruyenMaService.getAll();
		for(ChapterTruyenMa chapterTruyenMa : lstChapterTruyenMa){
			long idChapterTruyenMa = chapterTruyenMa.getId();
			String newContent = chapterTruyenMa.getContent();
			newContent = newContent.replace("thegioima.com", "truyenz.com").replace("thế giới ma chấm com", "truyenz.com");
			chapterTruyenMaService.update(idChapterTruyenMa, newContent);
		}
		return rs;
	}
	//replace chapter tam
	@RequestMapping("/chapterTam/replace-link.htm")
	public String replaceChapterTamTGM(){
		String rs = "redirect:/quan-ly/chapterTruyenMa/danh-sach-tat-ca-chapter-truyen.htm";
		List<ChapterTam> lstChapterTam = chapterTamService.getLstFromSourceBy(51);
		for(ChapterTam chapterTam : lstChapterTam){
			long idChapterTam = chapterTam.getId();
			String linkSource = chapterTam.getLinkSource();
			linkSource = linkSource.replace("http://thegioima.com/http://thegioima.com", "http://thegioima.com");
			int endIndex = linkSource.lastIndexOf("2011")+4;
			String linkSource1 = linkSource.substring(0, endIndex);
			String linkSource2 = linkSource.substring(endIndex, linkSource.length());
			linkSource = linkSource1+"/"+linkSource2;
			chapterTamService.updateLinkSource(idChapterTam, linkSource);
		}
		return rs;
	}
	
	/********************************************************************
	 * 
	 * thivien.net
	 * 
	 *********************************************************************/
	
	//lay tho
	@RequestMapping("/lay-tap-tho.htm")
	public String getAlbumThiVien(Model model,@RequestParam String url) {
		List<TapThoModel> lstItem = jsoupService.getAlbumThiVien(url);
		Collections.sort(lstItem, new TapThoModel.CompId());
		int totalSize = lstItem.size();
		model.addAttribute("lstItem", lstItem);
		model.addAttribute("totalSize", totalSize);
		return "/data/thivien/lstAlbum";
	}
}
